当前位置:网站首页>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];
}
边栏推荐
- [100 cases of JVM tuning practice] 03 -- four cases of JVM heap tuning
- Deep learning mathematics foundation
- How to print mybats log plug-in using XML file
- 守望先锋世界观架构 ——(一款好的游戏是怎么来的)
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- #gStore-weekly | gStore源码解析(四):安全机制之黑白名单配置解析
- Machine learning notes - time series prediction research: monthly sales of French champagne
- Excel查找一列中的相同值,删除该行或替换为空值
- [fluent] dart data type (VaR data type | object data type)
- 2022.7.1-----leetcode.241
猜你喜欢

Mysql高级篇学习总结8:InnoDB数据存储结构页的概述、页的内部结构、行格式

全链路数字化转型下,零售企业如何打开第二增长曲线

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors

What is 9D movie like? (+ common sense of dimension space)

yolov3 训练自己的数据集之生成train.txt

STM32G0 USB DFU 升级校验出错-2
![[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning](/img/59/6c776e0607a52962b72fbea2e64c8e.png)
[100 cases of JVM tuning practice] 02 - five cases of virtual machine stack and local method stack tuning

Use cheat engine to modify money, life and stars in Kingdom rush

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
随机推荐
以太网PHY层芯片LAN8720A简介
使用CLion编译OGLPG-9th-Edition源码
mysql备份后缀是什么_mysql备份还原
How performance testing creates business value
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
电脑使用哪个录制视频软件比较好
Excel如何进行隔行复制粘贴
golang:[]byte转string
2022 compilation principle final examination recall Edition
R语言ggplot2可视化:可视化折线图、使用labs函数为折线图添加自定义的X轴标签信息
Novice must see, click two buttons to switch to different content
How can retail enterprises open the second growth curve under the full link digital transformation
仿京东放大镜效果(pink老师版)
GMapping代码解析[通俗易懂]
Codeworks 5 questions per day (1700 average) - day 4
yolov3 训练自己的数据集之生成train.txt
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
ICDE 2023|TKDE Poster Session(CFP)
数据降维——主成分分析