当前位置:网站首页>2022.7.1-----leetcode. two hundred and forty-one
2022.7.1-----leetcode. two hundred and forty-one
2022-07-02 19:19:00 【Lu 727】
// Three operators
static final int ADDITION = -1;
static final int SUBTRACTION = -2;
static final int MULTIPLICATION = -3;
public List<Integer> diffWaysToCompute(String expression) {
List<Integer> ops = new ArrayList<Integer>();
// Split expressions into numbers and operators
for (int i = 0; i < expression.length();) {
if (!Character.isDigit(expression.charAt(i))) {
if (expression.charAt(i) == '+') {
ops.add(ADDITION);
} else if (expression.charAt(i) == '-') {
ops.add(SUBTRACTION);
} else {
ops.add(MULTIPLICATION);
}
i++;
} else {
int t = 0;
while (i < expression.length() &&Character.isDigit(expression.charAt(i)))
{
t = t * 10 + expression.charAt(i) - '0';
i++;
}
ops.add(t);
}
}
// Record [l,r] Result of expression
List<Integer>[][] dp = new List[ops.size()][ops.size()];
for (int i = 0; i < ops.size(); i++) {
for (int j = 0; j < ops.size(); j++) {
dp[i][j] = new ArrayList<Integer>();
}
}
return dfs(dp, 0, ops.size() - 1, ops);
}
public List<Integer> dfs(List<Integer>[][] dp, int l, int r, List<Integer> ops) {
// Non empty indicates that the expression has been solved
if (dp[l][r].isEmpty()) {
if (l == r) {
dp[l][r].add(ops.get(l));// There's only one number
} else {
// By the end of i+1 Bit operator is split , Solve in the left and right sub formulas respectively
for (int i = l; i < r; i += 2) {
List<Integer> left = dfs(dp, l, i, ops);
List<Integer> right = dfs(dp, i + 2, r, ops);
// This operator is the last step , The left and right subformulas should combine all possible results in pairs
for (int lv : left) {
for (int rv : right) {
if (ops.get(i + 1) == ADDITION) {
dp[l][r].add(lv + rv);
} else if (ops.get(i + 1) == SUBTRACTION) {
dp[l][r].add(lv - rv);
} else {
dp[l][r].add(lv * rv);
}
}
}
}
}
}
return dp[l][r];
}
边栏推荐
- MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
- golang:[]byte转string
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- Progress progress bar
- [100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning
- Processing strategy of message queue message loss and repeated message sending
- 2022 compilation principle final examination recall Edition
- 拦截器与过滤器的区别
- [fluent] dart data type (VaR data type | object data type)
猜你喜欢
性能测试如何创造业务价值
[test development] software testing - concept
新手必看,點擊兩個按鈕切換至不同的內容
Thread application instance
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
高频面试题
Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式
[100 cases of JVM tuning practice] 01 - introduction of JVM and program counter
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
Deep learning mathematics foundation
随机推荐
PHP非对称加密方法私钥及公钥加密解密的方法
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
移动机器人路径规划:人工势场法[通俗易懂]
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
Excel如何进行隔行复制粘贴
codeforces每日5题(均1700)-第四天
ICDE 2023|TKDE Poster Session(CFP)
Markdown basic grammar
Talk about the design of red envelope activities in e-commerce system
R语言dplyr包filter函数筛选dataframe数据、如果需要筛选的数据列(变量)名称中包含引号则需要使用!!sym语法处理、否则因为无法处理引号筛选不到任何数据
【测试开发】软件测试—概念篇
FastDFS安装
GMapping代码解析[通俗易懂]
R语言ggplot2可视化:gganimate包创建动态柱状图动画(gif)、使用transition_states函数在动画中沿给定维度逐步显示柱状图
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
R language uses Cox of epidisplay package Display function obtains the summary statistical information of Cox regression model (risk rate HR, adjusted risk rate and its confidence interval, P value of
MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree
juypter notebook 修改默认打开文件夹以及默认浏览器
使用xml文件打印mybaties-log插件的方式
Binary operation