[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 ...
随机推荐
- Effective C++ 笔记三 资源管理
条款13:以对象管理资源 许多资源被动态分配于heap内而后被用于单一区块或函数内.它们应该在控制流离开那个区块或函数时被释放.标准程序库提供的auto_ptr正是针对这种形式而设计的特制产品.aut ...
- SQL_server 的基本操作
1.---------------数据库基本操作 主键 : 1.不重复 2.不为NULL外键 1.取消重复行(消除完全一样的行,保留一行)select distinct cloumname1,clou ...
- JVM垃圾回收理论知识
- MongoDB的索引
一.索引详讲 索引是什么,索引就好比一本书的目录,当我们想找某一章节的时候,通过书籍的目录可以很快的找到,所以适当的加入索引可以提高我们查询的数据的速度. 准备工作,向MongoDB中插入20000条 ...
- 第一节 WCF概述
主要内容: 1.什么是WCF? 2.WCF的背景介绍. 引例:(WCF用来解决什么事情) 一家汽车租赁公司决定创建一个新的应用程序,用于汽车预定 • 该租车预定应用程序的创建者知道,应用程序所实现的业 ...
- 【Windows】Windows中的数据类型以及命名
一.大写标示符 Windows中的很多标识符都是以两个或者三个大写字母作为前缀的,且其后紧跟一个下划线.这些标识符都是常量数值,前缀表明该常量的一般类别.如下 前缀 常量 CS(Class Style ...
- jsp中<c:if>与<s:if>的区别
<c:if>时jstl标签,一般与el表达式一起使用,参考http://www.360doc.com/content/11/1121/16/7874148_166229306.shtml ...
- 如何生成publish windows app 用到的 pfx 文件
参考文章 https://msdn.microsoft.com/en-us/library/windows/desktop/jj835832(v=vs.85).aspx 1.在项目中查找.appxma ...
- 对有状态bean和无状态bean的理解(转)
现实中,很多朋友对两种session bean存在误解,认为有状态是实例一直存在,保存每次调用后的状态,并对下一次调用起作用,而认为无状态是每次调用实例化一次,不保留用户信息.仔细分析并用实践检验后, ...
- Codevs 1065 01字符串
1065 01字符串 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 传送门 题目描述 Description 输出仅有0和1组成的长度为n的字符串,并且其中不能含有 ...