当前位置:网站首页>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
边栏推荐
猜你喜欢
8大模块、40个思维模型,打破思维桎梏,满足你工作不同阶段、场景的思维需求,赶紧收藏慢慢学
There is a cow, which gives birth to a heifer at the beginning of each year. Each heifer has a heifer at the beginning of each year since the fourth year. Please program how many cows are there in the
【數字IC驗證快速入門】20、SystemVerilog學習之基本語法7(覆蓋率驅動...內含實踐練習)
如何在opensea批量发布NFT(Rinkeby测试网)
Ctfshow, information collection: web2
[机缘参悟-40]:方向、规则、选择、努力、公平、认知、能力、行动,读3GPP 6G白皮书的五层感悟
什么是数据泄露
[server data recovery] a case of RAID data recovery of a brand StorageWorks server
Ctfshow, information collection: web9
【数字IC验证快速入门】18、SystemVerilog学习之基本语法5(并发线程...内含实践练习)
随机推荐
2. Basic knowledge of golang
Stm32f103c8t6 PWM drive steering gear (sg90)
Do you know the relationship between the most important indicators of two strong wind control and the quality of the customer base
什么是pv和uv? pv、uv
[quickstart to Digital IC Validation] 20. Basic syntax for system verilog Learning 7 (Coverage Driven... Including practical exercises)
Use cpolar to build a business website (2)
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
Niuke real problem programming - day20
Ctfshow, information collection: web10
[quick start for Digital IC Validation] 26. Ahb - sramc (6) for system verilog project practice (Basic Points of APB Protocol)
Runnable是否可以中断
Excerpted words
Niuke real problem programming - day13
Mathematical modeling -- what is mathematical modeling
[server data recovery] data recovery case of raid failure of a Dell server
Ctfshow, information collection: web12
Unity's ASE achieves full screen sand blowing effect
[quick start of Digital IC Verification] 26. Ahb-sramc of SystemVerilog project practice (6) (basic points of APB protocol)
Ctfshow, information collection: web5
CTFshow,信息搜集:web7