当前位置:网站首页>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];
}
边栏推荐
猜你喜欢

消息队列消息丢失和消息重复发送的处理策略
![[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
![[daily question] first day](/img/8c/f25cddb6ca86d44538c976fae13c6e.png)
[daily question] first day

数据降维——主成分分析

How can retail enterprises open the second growth curve under the full link digital transformation

论文导读 | 关于将预训练语言模型作为知识库的分析与批评

How to copy and paste interlaced in Excel

MySQL advanced learning summary 7: MySQL data structure - Comparison of hash index, AVL tree, B tree and b+ tree

新手必看,点击两个按钮切换至不同的内容

Imitation Jingdong magnifying glass effect (pink teacher version)
随机推荐
R language dplyr package filter function filters dataframe data. If the name of the data column (variable) to be filtered contains quotation marks, you need to use!! SYM syntax processing, otherwise n
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
Excel查找一列中的相同值,删除该行或替换为空值
Tips for material UV masking
Compile oglpg-9th-edition source code with clion
Learning summary of MySQL advanced 6: concept and understanding of index, detailed explanation of b+ tree generation process, comparison between MyISAM and InnoDB
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Mysql高级篇学习总结6:索引的概念及理解、B+树产生过程详解、MyISAM与InnoDB的对比
R language dplyr package Na_ The if function converts the control in the vector value into the missing value Na, and converts the specified content into the missing value Na according to the mapping r
Page title component
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
[test development] takes you to know what software testing is
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
#gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
SIFT特征点提取「建议收藏」
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
STM32G0 USB DFU 升级校验出错-2
Crypto usage in nodejs
C文件输入操作