当前位置:网站首页>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
边栏推荐
- Mathematical modeling -- what is mathematical modeling
- 知否|两大风控最重要指标与客群好坏的关系分析
- 【数字IC验证快速入门】23、SystemVerilog项目实践之AHB-SRAMC(3)(AHB协议基本要点)
- @Introduction and three usages of controlleradvice
- 【OBS】RTMPSockBuf_ Fill, remote host closed connection.
- [deep learning] image hyperspectral experiment: srcnn/fsrcnn
- [机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
- 【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
- With 8 modules and 40 thinking models, you can break the shackles of thinking and meet the thinking needs of different stages and scenes of your work. Collect it quickly and learn it slowly
- 【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
猜你喜欢
【OBS】RTMPSockBuf_ Fill, remote host closed connection.
Niuke real problem programming - day15
2. Heap sort "hard to understand sort"
[quick start of Digital IC Verification] 20. Basic grammar of SystemVerilog learning 7 (coverage driven... Including practical exercises)
Niuke real problem programming - Day17
【深度学习】图像超分实验:SRCNN/FSRCNN
Use cpolar to build a business website (2)
Niuke real problem programming - day18
Ctfshow, information collection: web14
CTFshow,信息搜集:web14
随机推荐
什么是数据泄露
Ctfshow, information collection: web2
2022年5月互联网医疗领域月度观察
leetcode 241. Different Ways to Add Parentheses 为运算表达式设计优先级(中等)
Ctfshow, information collection: web4
【数字IC验证快速入门】25、SystemVerilog项目实践之AHB-SRAMC(5)(AHB 重点回顾,要点提炼)
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
从 1.5 开始搭建一个微服务框架链路追踪 traceId
2. Heap sort "hard to understand sort"
MongoD管理数据库的方法介绍
使用cpolar建立一个商业网站(2)
【目标检测】YOLOv5跑通VOC2007数据集
Oracle control file loss recovery archive mode method
#HPDC智能基座人才发展峰会随笔
Wechat applet 01
数学建模——什么是数学建模
Pat grade a 1103 integer factorizatio
[follow Jiangke University STM32] stm32f103c8t6_ PWM controlled DC motor_ code
[understanding of opportunity -40]: direction, rules, choice, effort, fairness, cognition, ability, action, read the five layers of perception of 3GPP 6G white paper
Connecting FTP server tutorial