(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 ...
随机推荐
- 可视化SNV安装
以前也安装过非图形化的SVN SERVER,但大多都需要比较复杂的配置,而且操作不太友好,所以其拥有可视化功能就比较重要了. 好了,开始干活吧. 说明:测试机为开发爬虫用的服务器,配置不高 准备工作: ...
- sklearn基础知识-准备阶段
6.标签特征二元化 处理分类变量还有另一种方法,不需要通过OneHotEncoder,我们可以用LabelBinarizer. 这是一个阈值与分类变量组合的方法. In [1]: from sklea ...
- ORACLE 常用数值函数
1 ABS(n)返回数值弄参数的绝对值.它接受一个数值型值作为输入参数,或者任何可以隐式地转换为数值型值的值.并且返回数值型值的绝对值. Select abs(-1) from dual ABS(-1 ...
- Keepalived+MySQL实现高可用(转)
http://www.cnblogs.com/wingsless/p/4033093.html MHA高可用 http://www.cnblogs.com/gomysql/p/3856484.ht ...
- jQuery.KinSlideshow焦点图轮换
兼容IE6/IE7/IE8/IE9,FireFox,Chrome*,Opera的 jQuery. KinSlideshow幻灯片插件,功能很多 ,基本能满足你在网页上使用幻灯片(焦点图)效果. 演示网 ...
- android学习笔记15——Galley
Gallery==>画廊视图 Gallery和Spinnery父类相同——AbsSpinner,表明Garrey和Spinner都是一个列表框. 两者之间的区别是:Spinner显示的是一个垂直 ...
- sql里将重复行数据合并为一行,数据用逗号分隔
一.定义表变量 DECLARE @T1 table ( UserID int , UserName ), CityName ) ); ,'a','上海') ,'b','北京') ,'c','上海') ...
- js常见数字处理整理
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- TextBox仿Foxmail收件人删除效果
场景: 发邮件,添加收件人后,删除时直接删除整个收件人. 添加事件: 文本框实现,需要添加以下4个事件: txtQsdw_TextChanged txtQsdw_MouseDown txtQsdw_M ...
- linq简介
语言集成查询(Language INtegrated Query,LINQ)是一项微软技术,新增一种自然查询的SQL语法到.NET Framework的编程语言中,可支持Visual Basic .N ...