当前位置:网站首页>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 好事多磨,又要延期一周
边栏推荐
- Computer win7 system desktop icon is too large, how to turn it down
- 回归测试的分类
- Spatiotemporal deformable convolution for compressed video quality enhancement (STDF)
- Zhiting doesn't use home assistant to connect Xiaomi smart home to homekit
- [Yugong series] go teaching course 005 variables in July 2022
- What is the process of ⼀ objects from loading into JVM to being cleared by GC?
- Win10 or win11 taskbar, automatically hidden and transparent
- CPU与chiplet技术杂谈
- 半小时『直播连麦搭建』动手实战,大学生技术岗位简历加分项get!
- 属性关键字ServerOnly,SqlColumnNumber,SqlComputeCode,SqlComputed
猜你喜欢
CTFshow,信息搜集:web9
MySQL installation configuration 2021 in Windows Environment
Promoted to P8 successfully in the first half of the year, and bought a villa!
Ctfshow, information collection: web10
leetcode:648. Word replacement [dictionary tree board + find the shortest matching prefix among several prefixes]
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条...
asp. Netnba information management system VS development SQLSERVER database web structure c programming computer web page source code project detailed design
暑期安全很重要!应急安全教育走进幼儿园
Instructions d'utilisation de la trousse de développement du module d'acquisition d'accord du testeur mictr01
Apache多个组件漏洞公开(CVE-2022-32533/CVE-2022-33980/CVE-2021-37839)
随机推荐
Novel Slot Detection: A Benchmark for Discovering Unknown Slot Types in the Dialogue System
Stream learning notes
Es log error appreciation -trying to create too many buckets
[Yugong series] go teaching course 005 variables in July 2022
用于增强压缩视频质量的可变形卷积密集网络
How does the database perform dynamic custom sorting?
Deformable convolutional dense network for enhancing compressed video quality
Huawei cloud database DDS products are deeply enabled
Ctfshow, information collection: web5
广州开发区让地理标志产品助力乡村振兴
安恒堡垒机如何启用Radius双因素/双因子(2FA)身份认证
WebRTC 音频抗弱网技术(上)
Niuke real problem programming - Day11
Niuke real problem programming - Day17
Ascend 910 realizes tensorflow1.15 to realize the Minist handwritten digit recognition of lenet network
C 6.0 language specification approved
How to enable radius two factor / two factor (2fa) identity authentication for Anheng fortress machine
JSON parsing instance (QT including source code)
「2022年7月」WuKong编辑器更版记录
Change win10 Screensaver