当前位置:网站首页>2022.7.1-----leetcode.241
2022.7.1-----leetcode.241
2022-07-02 17:54:00 【路Lu727】
//三种运算符
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>();
//将表达式拆成数字和运算符
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);
}
}
//记录[l,r]表达式的结果
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) {
//非空说明已经求解过该表达式
if (dp[l][r].isEmpty()) {
if (l == r) {
dp[l][r].add(ops.get(l));//只有一个数字
} else {
//以第i+1位的运算符为分割,分别在左右子式进行求解
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);
//该运算符作为最后一步,左右子式应将所有可能结果的进行两两组合
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];
}
边栏推荐
- 二进制操作
- 27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
- Introduction to sap s/4hana OData mock service
- 消息队列消息丢失和消息重复发送的处理策略
- 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
- yolov3 训练自己的数据集之生成train.txt
- [Yugong series] July 2022 go teaching course 001 introduction to go language premise
- [daily question] the next day
- [0701] [paper reading] allowing data imbalance issue with perforated input during influence
- 【测试开发】一文带你了解什么是软件测试
猜你喜欢
The difference between interceptor and filter
彻底搞懂基于Open3D的点云处理教程!
UML 类图
9D电影是怎样的?(+维度空间常识)
开发固定资产管理系统,开发固定资产管理系统用什么语音
How to clean up discarded PVs and their corresponding folders
Markdown基础语法
迷你高尔夫球场:伦敦休闲旅游好去处
How performance testing creates business value
27: Chapter 3: develop Passport Service: 10: [registration / login] interface: after the registration / login is OK, save the user session information (uid, utoken) to redis and cookies; (one main poi
随机推荐
How to clean up discarded PVs and their corresponding folders
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
为什么要做企业固定资产管理系统,企业如何加强固定资产管理
Hongmeng's fourth learning
Progress progress bar
SLC、MLC、TLC 和 QLC NAND SSD 之间的区别:哪个更好?
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
Singapore summer tourism strategy: play Singapore Sentosa Island in one day
Processing strategy of message queue message loss and repeated message sending
Installation of thingsboard, an open source IOT platform
FastDFS安装
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
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
Talk about the design of red envelope activities in e-commerce system
消息队列消息丢失和消息重复发送的处理策略
Stm32g0 USB DFU upgrade verification error -2
ICDE 2023|TKDE Poster Session(CFP)
【测试开发】软件测试—概念篇
How to play when you travel to Bangkok for the first time? Please keep this money saving strategy
材质UV遮罩的技巧