当前位置:网站首页>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 好事多磨,又要延期一周
边栏推荐
- ⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
- 「2022年7月」WuKong编辑器更版记录
- Lidar Knowledge Drop
- Small game design framework
- Notes HCIA
- Full details of efficientnet model
- Shengteng experience officer Episode 5 notes I
- Ascend 910 realizes tensorflow1.15 to realize the Minist handwritten digit recognition of lenet network
- Today's sleep quality record 78 points
- [today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
猜你喜欢
IDA pro逆向工具寻找socket server的IP和port
MySQL installation configuration 2021 in Windows Environment
AWS learning notes (III)
暑期安全很重要!应急安全教育走进幼儿园
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?
Notes HCIA
Deformable convolutional dense network for enhancing compressed video quality
Introduction and use of Kitti dataset
asp.netNBA信息管理系统VS开发sqlserver数据库web结构c#编程计算机网页源码项目详细设计
Ctfshow, information collection: Web3
随机推荐
Integer learning
安恒堡垒机如何启用Radius双因素/双因子(2FA)身份认证
Data Lake (IX): Iceberg features and data types
智汀不用Home Assistant让小米智能家居接入HomeKit
CTFshow,信息搜集:web7
Lidar knowledge drops
Ctfshow, information collection: web6
大厂做开源的五大痛点
Ctfshow, information collection: web7
[today in history] July 7: release of C; Chrome OS came out; "Legend of swordsman" issued
Niuke real problem programming - Day10
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
Bits and Information & integer notes
"July 2022" Wukong editor update record
CTFshow,信息搜集:web14
Stm32cubemx, 68 sets of components, following 10 open source protocols
拜拜了,大厂!今天我就要去厂里
AWS learning notes (III)
Computer win7 system desktop icon is too large, how to turn it down
⼀个对象从加载到JVM,再到被GC清除,都经历了什么过程?