当前位置:网站首页>leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
leetcode 241. Different ways to add parentheses design priority for operational expressions (medium)
2022-07-07 15:35:00 【InfoQ】
One 、 The main idea of the topic
- 1 <= expression.length <= 20
- expression By numbers and operators '+'、'-' and '*' form .
- All integer values in the input expression are in the range [0, 99]
Two 、 Their thinking
3、 ... and 、 How to solve the problem
3.1 Java Realization
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;
}
}
Four 、 Summary notes
- 2022/7/7 ' , Another week's delay
边栏推荐
- Niuke real problem programming - Day10
- Nacos一致性协议 CP/AP/JRaft/Distro协议
- Unity's ASE achieves full screen sand blowing effect
- 众昂矿业:萤石继续引领新能源市场增长
- 2.Golang基础知识
- Apache multiple component vulnerability disclosure (cve-2022-32533/cve-2022-33980/cve-2021-37839)
- 【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
- Ctfshow, information collection: web4
- CTFshow,信息搜集:web10
- Briefly describe the working principle of kept
猜你喜欢

微信小程序 01

Niuke real problem programming - Day11

什么是数据泄露
MongoD管理数据库的方法介绍

Change win10 Screensaver

Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base

写一篇万字长文《CAS自旋锁》送杰伦的新专辑登顶热榜

Win10 or win11 taskbar, automatically hidden and transparent

Bye, Dachang! I'm going to the factory today

Ctfshow, information collection: web8
随机推荐
大表delete删数据导致数据库异常解决
如何在opensea批量发布NFT(Rinkeby测试网)
Niuke real problem programming - Day12
Qu'est - ce qu'une violation de données
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
2. Heap sort "hard to understand sort"
Niuke real problem programming - day20
[quick start of Digital IC Verification] 22. Ahb-sramc of SystemVerilog project practice (2) (Introduction to AMBA bus)
Niuke real problem programming - Day9
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
2. 堆排序『较难理解的排序』
CTFshow,信息搜集:web7
Bye, Dachang! I'm going to the factory today
CTFshow,信息搜集:web10
CTFshow,信息搜集:web3
#HPDC智能基座人才发展峰会随笔
简述keepalived工作原理
Mathematical modeling -- what is mathematical modeling
Compile advanced notes
Ctfshow, information collection: web7