[LeetCode#241]Different Ways to Add Parentheses
Problem:
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]
Analysis:
At first glance, there problem is really complex!!! But once you get the point, you feel so surprised that this problem is so elegant and simple. Basic idea: divide and conquer.
Assumption 1: input = a - b + c + e * f
How could we add parenthess onto the input string? Add all parenthesses at one time?
That's too complex...
How about add parenthess recursively???
For each input, we only add two parenthess, then pass the task of adding parenthess into next level.
Step 1: input = (a - b) + (c + e * f)
Step 2: add_compute(a - b), add_compute(c + e * f) Key 1: the valid way to add bracket, choice a operator, then cut into two parts.
if (c == '+' || c == '-' || c == '*') {
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
...
} Key 2: Use the number string(not include operand) as base case! Since we always divide the input string against a operator, we must be able to reach the base case: "input is a string representation of number".
if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
ret.add(Integer.valueOf(input));
return ret;
}
Note: iff use input.indexOf('+') must in the form of input.indexOf('+') != -1. Key 3: use operand over the return value from child branch.
for (int m = 0; m < left.size(); m++) {
for (int n = 0; n < right.size(); n++) {
int num1 = left.get(m);
int num2 = right.get(n);
if (c == '+')
ret.add(num1 + num2);
else if (c == '-')
ret.add(num1 - num2);
else
ret.add(num1 * num2);
}
}
Wrong solution:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret = new ArrayList<Integer> ();
int len = input.length();
if (len == 1) {
ret.add(Integer.valueOf(input.charAt(0) + ""));
return ret;
}
for (int i = 0; i < len; i++) {
char c = input.charAt(i);
if (c == '+' || c == '-' || c == '*') {
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
for (int m = 0; m < left.size(); m++) {
for (int n = 0; n < right.size(); n++) {
if (c == '+')
ret.add(m + n);
else if (c == '-')
ret.add(m - n);
else
ret.add(m * n);
}
}
}
}
return ret;
}
}
Mistake analysis:
Mistake 1:
The stereotype of using CharAt(). In this problem, a number could be represented by using multi charcacters together.
Fix:
if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
ret.add(Integer.valueOf(input));
return ret;
} Mistake 2:
Forget to get the number from return list, but use index.
Fix:
int num1 = left.get(m);
int num2 = right.get(n);
if (c == '+') {
...
}
Solution:
public class Solution {
public List<Integer> diffWaysToCompute(String input) {
List<Integer> ret = new ArrayList<Integer> ();
int len = input.length();
if (!(input.contains("+") || input.contains("-") || input.contains("*"))) {
ret.add(Integer.valueOf(input));
return ret;
}
for (int i = 0; i < len; i++) {
char c = input.charAt(i);
if (c == '+' || c == '-' || c == '*') {
List<Integer> left = diffWaysToCompute(input.substring(0, i));
List<Integer> right = diffWaysToCompute(input.substring(i+1, input.length()));
for (int m = 0; m < left.size(); m++) {
for (int n = 0; n < right.size(); n++) {
int num1 = left.get(m);
int num2 = right.get(n);
if (c == '+')
ret.add(num1 + num2);
else if (c == '-')
ret.add(num1 - num2);
else
ret.add(num1 * num2);
}
}
}
}
return ret;
}
}
[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 ...
- (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 ...
- 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 ...
随机推荐
- enable cors in spring mvc with swagger
a. In controller add @CrossOrigin(origins = "http://localhost:8080") b. In mvc-servlet.xml ...
- jquery.validate.js默认配置,jquery.validate.js自定义提示信息
jquery.validate.js默认配置,jquery.validate.js自定义提示信息 配置jQuery.validator默认的处理方法 >>>>>>& ...
- Java基础--继承方法调用顺序
最近因为面试的原因,回过头来复习基础的知识,都忘光了,准备买本面试书回来啃. 我先把自己测试的结论总结写出来,以后忘记再来看看 如果b类继承自a类,在main方法中new出b的对象(不带参数),那么他 ...
- HTML+CSS 整站 步骤
文件夹管理: CSS JS img font html 根据设计图,划分区块 ,即页面布局 重置样式 ;padding:0;} 写main.css 注意:1 距离尽量使用偶数,避免奇数 2 在使用定 ...
- [转载]SQL字符串处理函数大全
[转载]http://www.cnblogs.com/andy2005/archive/2007/12/04/981864.html select语句中只能使用sql函数对字段进行操作(链接sql s ...
- 文件上传利器SWFUpload使用指南
这里就不再介绍什么是SWFUpload啦,简单为大家写一个简单关于SWFUpload的Demo. 1.把SWFUpload 相关的文件引用进来 2.创建upload.aspx页面(页面名称可自定义), ...
- MySQL常见问题汇总(原创)
本文记录了使用Mysql时遇到的问题,持续更新中... 1.在windows命令行下登录mysql时报错: C:\Program Files\MySQL\MySQL Server 5.0\bin> ...
- UIScrollView -2(UIScrollView 与 UIPageControl的使用): 分页查看图片
1.初始化UIScrollView 2.设置初始化出来的UIScrollView的contentSize: myscrollview.contentSize =CGSizeMake(CGRectGet ...
- .NET开源工程推荐(Accord,AForge,Emgu CV)
本人用C#开发了一些项目,下面的开源工程给了我很大的帮助——详细的源代码介绍加丰富的实例运用,是非常不错的学习资源,分享给大家,同时附上我的相关开发项目. Accord.NET The ...
- Codevs 1140 Jam的计数法 2006年NOIP全国联赛普及组
1140 Jam的计数法 2006年NOIP全国联赛普及组 传送门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Jam是个喜欢标 ...