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的更多相关文章

  1. 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 ...

  2. [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  3. (medium)LeetCode 241.Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. 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 ...

  5. LeetCode 241. Different Ways to Add Parentheses为运算表达式设计优先级 (C++)

    题目: Given a string of numbers and operators, return all possible results from computing all the diff ...

  6. 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]* ...

  7. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

  8. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  9. LC 241. Different Ways to Add Parentheses

    Given a string of numbers and operators, return all possible results from computing all the differen ...

随机推荐

  1. C#面向对象(二)

    一:抽象方法 1. 在面向对象编程语言中抽象方法指一些只有方法声明,而没有具体方法体的方法.抽象方法一般存在于抽象类或接口中. 在一些父类中,某些行为不是非常明确,因此无法用代码来具体实现,但是类还必 ...

  2. 一个js 变量作用域问题

    一个js 域问题,有一本书 叫 javasrcip pattert 好像是,写的很好,, <!DOCTYPE html> <html> <head lang=" ...

  3. CI框架篇之基础篇(2)

    CodeIgniter 的基础了解了后,现在就来对这个框架进行预热 CodeIgniter 配置 理论是不用配置,直接拷贝到服务器目录下即可运行 CodeIgniter 安装分为四个步骤: 1. 解压 ...

  4. 关于ASP.Net中路径的问题

    比如你的工程是Webapplication1(url是:http://localhost/webapplication1/webform1.aspx) Request.ApplicationPath ...

  5. [验证码实现] Captcha 验证码类,一个很个性的验证码类 (转载)

    点击下载 Captcha.zip /// <summary> /// 类说明:条码生成类 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=htt ...

  6. 《你不常用的c#之四》:Array的小抽屉ArraySegment

    转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6200060 一:)略谈      ArraySegment顾名思义就是Array区块 ...

  7. Synchronized vs SyncRoot

    我们知道,在.net的一些集合类型中,譬如Hashtable和ArrayList,都有Synchronized静态方法和SyncRoot实例方法,他们之间有联系吗?我怎么才能用好他们呢?我们以Hash ...

  8. CoreAnimation1-图层树、寄宿图以及图层几何学

    (一)图层的树状结构 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Kit这么一个不怎么和动画有关的名字演变而来,所以做动画这只 ...

  9. (转) c# ExecuteNonQuery() 返回值 -1

    这是之前我遇到问题,在网上找解决方法时找到的,当时复制到txt文档了,今天整理笔记又看到了,贴出来,便于以后查阅.原文的作者没记住~~ 查询某个表中是否有数据的时候,如果用ExecuteNonQuer ...

  10. linux shell获取时间

    获得当天的日期 date +%Y-%m-%d 输出: 2011-07-28 将当前日期赋值给DATE变量DATE=$(date +%Y%m%d) 有时候我们需要使用今天之前或者往后的日期,这时可以使用 ...