(medium)LeetCode 241.Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+, - and *.
Example 1
Input: "2-1-1".
((2-1)-1) = 0
(2-(1-1)) = 2
Output: [0, 2]
Example 2
Input: "2*3-4*5"
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10
Output: [-34, -14, -10, -10, 10]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
解法思想:递归
代码如下:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret=new LinkedList<Integer>();
int len=input.length();
for(int i=0;i<len;i++){
if(input.charAt(i)=='-'||
input.charAt(i)=='*'||
input.charAt(i)=='+'){
String part1=input.substring(0,i);
String part2=input.substring(i+1);
List<Integer> part1Ret=diffWaysToCompute(part1);
List<Integer> part2Ret=diffWaysToCompute(part2);
for(Integer p1:part1Ret){
for(Integer p2:part2Ret){
int c=0;
switch(input.charAt(i)){
case '+':c=p1+p2;
break;
case '-':c=p1-p2;
break;
case '*': c=p1*p2;
}
ret.add(c);
}
}
}
}
if(ret.size()==0){
ret.add(Integer.valueOf(input));
}
return ret;
}
}
运行结果:

(medium)LeetCode 241.Different Ways to Add Parentheses的更多相关文章
- LN : leetcode 241 Different Ways to Add Parentheses
lc 241 Different Ways to Add Parentheses 241 Different Ways to Add Parentheses Given a string of num ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)
https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)
题目: Given a string of numbers and operators, return all possible results from computing all the diff ...
- leetcode 96. Unique Binary Search Trees 、95. Unique Binary Search Trees II 、241. Different Ways to Add Parentheses
96. Unique Binary Search Trees https://www.cnblogs.com/grandyang/p/4299608.html 3由dp[1]*dp[1].dp[0]* ...
- 241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
随机推荐
- ASP.NET MVC 控制器向View传值的三种方法
转自:http://www.cnblogs.com/shinima/p/3940452.html 1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResu ...
- 【转】java 自动装箱与拆箱
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- 使用Cyclone IV控制DDR2
根据你的DDR2手册配置好megacore,megacore会生成一个example top: 在quartus中运行megacore生成的xxx_pin_assignments.tcl,指定DDR2 ...
- 多用less命令,不会输入h查看对应的详细文档
在开发项目时候,难免要查看日志排查错误.之前只会用cat , more, less, tac, tail的简单功能, 但在实际工程中还是不够用的,至少效率很低.今天抽空看了下以下的博客,并实际进行了简 ...
- spring+springMVC+mybatis的框架项目基础环境搭建
上一个项目在后台用到spring+springMVC+mybatis的框架,先新项目初步需求也已经下来,不出意外的话,应该也是用这个框架组合. 虽然在之前activiti相关的学习中所用到的框架也是这 ...
- 在UIViewController中获得Container View里的embed viewController的引用
When you want to use a controller you use the UIStoryboard method instantiateViewControllerWithIdent ...
- USACO CHAPTER 1 1.1 Ride 水题
水题,主要是学习文件输入输出. /* ID: ijustwa1 LANG: C++ TASK: ride */ #include<cstdio> #include<cstring&g ...
- Oracle11G登录时提示:ORA-12557: TNS:协议适配器不可加载
原文 Oracle11G登录时提示:ORA-12557: TNS:协议适配器不可加载 初步分析是ORACLE_HOME设置错误引起的.前几天不小心看到系统环境变量中的其值为空,就手贱的加载了一个ora ...
- 1.运行Android Studio,一直提示:Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
1.解决问题办法:菜单栏,Tools -> Adnroid -> enable ADB integration勾上 2.暂时性的解决方案:在Android Studio中的:Prefere ...
- linux命令(12)uniq去重
转载地址:http://blog.51yip.com/shell/1022.html 实例详细说明linux下去除重复行命令uniq 一,uniq干什么用的 文本中的重复行,基本上不是我们所要的,所以 ...