当前位置:网站首页>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
边栏推荐
- 【深度学习】图像超分实验:SRCNN/FSRCNN
- Integer learning
- Nacos conformance protocol cp/ap/jraft/distro protocol
- Runnable是否可以中断
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- [make a boat diary] [shapr3d STL format to gcode]
- 如何在opensea批量发布NFT(Rinkeby测试网)
- Window环境下配置Mongodb数据库
- [deep learning] semantic segmentation experiment: UNET network /msrc2 dataset
- Typescript release 4.8 beta
猜你喜欢
Ctfshow, information collection: web14
【跟着江科大学Stm32】STM32F103C8T6_PWM控制直流电机_代码
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
居然从408改考自命题!211华北电力大学(北京)
Niuke real problem programming - Day10
Notes HCIA
Ctfshow, information collection: web12
【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
Ctfshow, information collection: web9
Niuke real problem programming - day18
随机推荐
Pat grade a 1103 integer factorizatio
Concurrency Control & NoSQL and new database
Mathematical modeling -- what is mathematical modeling
Used by Jetson AgX Orin canfd
从 1.5 开始搭建一个微服务框架链路追踪 traceId
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
【数字IC验证快速入门】24、SystemVerilog项目实践之AHB-SRAMC(4)(AHB继续深入)
CTFshow,信息搜集:web10
Nacos conformance protocol cp/ap/jraft/distro protocol
CTFshow,信息搜集:web7
Database exception resolution caused by large table delete data deletion
MySQL bit type resolution
Unity之ASE实现卡通火焰
【数字IC验证快速入门】29、SystemVerilog项目实践之AHB-SRAMC(9)(AHB-SRAMC SVTB Overview)
Ctfshow, information collection: web13
Niuke real problem programming - day15
What are the safest securities trading apps
Ctfshow, information collection: web1
使用Scrapy框架爬取网页并保存到Mysql的实现
避坑:Sql中 in 和not in中有null值的情况说明