当前位置:网站首页>leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
2022-07-07 12:56:00 【InfoQ】
一、题目大意
- 1 <= expression.length <= 20
- expression 由数字和算符 '+'、'-' 和 '*' 组成。
- 输入表达式中的所有整数值在范围 [0, 99]
二、解题思路
三、解题方法
3.1 Java实现
public class Solution {
public List<Integer> diffWaysToCompute(String expression) {
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < expression.length(); i++) {
char ope = expression.charAt(i);
if (ope == '+' || ope == '-' || ope == '*') {
List<Integer> left = diffWaysToCompute(expression.substring(0, i));
List<Integer> right = diffWaysToCompute(expression.substring(i + 1));
for (int l : left) {
for (int r : right) {
switch (ope) {
case '+':
ans.add(l + r);
break;
case '-':
ans.add(l - r);
break;
case '*':
ans.add(l * r);
break;
}
}
}
}
}
if (ans.isEmpty()) {
ans.add(Integer.valueOf(expression));
}
return ans;
}
}
四、总结小记
- 2022/7/7 好事多磨,又要延期一周
边栏推荐
- Es log error appreciation -trying to create too many buckets
- JS in the browser Base64, URL, blob mutual conversion
- Ctfshow, information collection: web1
- PD virtual machine tutorial: how to set the available shortcut keys in the parallelsdesktop virtual machine?
- Stream learning notes
- Lidar knowledge drops
- Integer learning
- CTFshow,信息搜集:web13
- 智汀不用Home Assistant让小米智能家居接入HomeKit
- Pytorch model trains practical skills and breaks through the bottleneck of speed
猜你喜欢

比尔·盖茨晒48年前简历:“没你们的好看”

Leetcode one question per day (636. exclusive time of functions)

Discussion on CPU and chiplet Technology

防火墙基础之服务器区的防护策略

Promoted to P8 successfully in the first half of the year, and bought a villa!

Computer win7 system desktop icon is too large, how to turn it down

13 ux/ui/ue best creative inspiration websites in 2022

CTFshow,信息搜集:web4

Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)

CPU与chiplet技术杂谈
随机推荐
Instructions for mictr01 tester vibrating string acquisition module development kit
因员工将密码设为“123456”,AMD 被盗 450Gb 数据?
Change win10 Screensaver
Ctfshow, information collection: web4
《微信小程序-进阶篇》组件封装-Icon组件的实现(一)
EfficientNet模型的完整细节
2022云顾问技术系列之高可用专场分享会
数据库如何进行动态自定义排序?
Cocos creator direction and angle conversion
Niuke real problem programming - day15
Ctfshow, information collection: web2
Stream learning notes
#yyds干货盘点# 解决名企真题:交叉线
上半年晋升 P8 成功,还买了别墅!
缓冲区溢出保护
Computer win7 system desktop icon is too large, how to turn it down
Classification of regression tests
在软件工程领域,搞科研的这十年!
Base64 encoding
Niuke real problem programming - day16