Different Ways to Add Parentheses——Leetcode
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]
题目大意:给定一个表达式,让你随便在上面加括号,然后算所有可能的结果。
解题思路:
这题如果顺着题目的意思,自己去加括号然后算表达式的值那就完了,掉坑里出不来了,其实就是分治,举个例子:
对表达式,找到一个运算符,然后计算式子两边的所有可能的值,然后把两边分别乘起来就可以了,递归的计算两边的值。
题目的要求就是计算所有的。
public static List<Integer> diffWaysToCompute(String input) {
List<Integer> res = new ArrayList<>();
if(input==null||input.length()==0){
return res;
}
for(int i=0;i<input.length();i++){
char c = input.charAt(i);
if(!Character.isDigit(c)){
List<Integer> pre = diffWaysToCompute(input.substring(0,i));
List<Integer> post = diffWaysToCompute(input.substring(i+1,input.length()));
for(int f : pre){
for(int l : post){
if(c=='+'){
res.add(f+l);
}else if(c=='-'){
res.add(f-l);
}else if(c=='*'){
res.add(f*l);
}
}
}
}
}
if(res.size()==0){
res.add(Integer.valueOf(input));
}
Collections.sort(res);
return res;
}
Different Ways to Add Parentheses——Leetcode的更多相关文章
- 【LeetCode】241. Different Ways to Add Parentheses
Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...
- 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 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 ...
- LC 241. Different Ways to Add Parentheses
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] 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 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- (medium)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 ...
随机推荐
- php 计算一个字符串在另一个字符串中出现的次数
<?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // ...
- C#中Hashtable、Dictionary详解以及写入和读取对比
转载:http://www.cnblogs.com/chengxingliang/archive/2013/04/15/3020428.html 在本文中将从基础角度讲解HashTable.Dicti ...
- Synchronized vs SyncRoot
我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?我们以Hash ...
- linux导入导出数据库方法 windows导入导出数据库方法
1.使用管理员账号(sys)登录查询字符集信息 第一步:查询LinuxOracle数据库的字符集 select userenv('language') from dual; 查询结果集可能为:AMER ...
- 171. Excel Sheet Column Number(C++)
171. Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as ...
- 找不好重现的bug的一个小技巧————守株待兔
最近碰到一个问题就是数据库中偶尔出现一条没有id的数据,可恨的是怎么也找不到重现这个问题的原因,只好换种方式来找了,那么就是我标题所说的守株待兔方法. 因为我发现出现bug的数据库里面的数据有个字段为 ...
- Chrome退出全屏问题
最近做了一个号称很炫的B/S展示软件,展示所用浏览器为Google Chrome. 要求展示时全屏,但是页面要有退出全屏按钮(液晶屏没有键盘). 搜索实现方式几乎前篇一律,即两个JS函数一个实现全屏一 ...
- cmd 窗口的复制粘贴
如下几种方法1.点击鼠标右键,选择标志,再点击左键拖动选择要复制的内容,然后回车即可复制被 选择的内容 2.点击鼠标右键,选择标志,再点击左键拖动选择要复制的内容,然后点击鼠标右键, 此时就把选择的内 ...
- js 中对象属性特性的描述
如何自定义属性的特性? 用对象.属性的特性和自定义的属性的特性有什么区别? 它的四大特性 writable enumerable configable 有什么区别? 先预习一个用对象.属性 ...
- 自己挖坑自己跳 之JsonMappingException: (was java.lang.NullPointerException) (through reference chain:)
在Web项目中,我们经常会设计一些与界面相对应的JavaBean作为Entity,而为了兼容前台传入的空值,有些字段我们会用包装类型而不是基本类型.可是往往我的Entity已经设计完成,很多时候我们会 ...