[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 ...
随机推荐
- RHEL7单独安装图形X11
RHEL7 默认是最小化安装(Minimal Install),没有图形界面,我们应该选择Server with GUI.若已错过此步骤,我们采用以下方式补充安装GUI界面. # yum group ...
- day-8
/* 倒数8天了 今天高考报名 两个班主任都来了23333 然后填班主任姓名就尴尬了23333 上午考试 挺不错的题目 都不是很水 但需要思考 而我是一脸的懵逼~~ 下午整理各种板子 主要是图论的 最 ...
- asp.net页面刷新等问题
windows.open 关闭当前页面刷新父页面实现() { 在子页面中 Page.ClientScript.RegisterStartupScript(this.GetType(), "a ...
- Java-struts2 之值栈问题
这里是根据一个小项目,将数据库的值查出来,然后在页面前台进行遍历的方法 放入值的几种方式: Struts2的三种存值取值的方式 值栈: 栈上下文: ActionContext: package com ...
- [IO] C# INI文件读写类与源码下载 (转载)
/// <summary> /// 类说明:INI文件读写类. /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url]http://www.sufei ...
- arcgis engine - 添加图例,指北针.
esri帮助提供了使用比例尺的方法: Working with map surrounds 主要代码为: public void AddMapSurround(IPageLayout pageLayo ...
- CI 笔记,借鉴的4个辅助自定义函数
在System的core的common.php中,借鉴的4个自定义函数, 摘自后盾网的CI教程 /** * 格式化打印函数 * @param [type] $arr [数组] * @return [t ...
- ASP.NET配置KindEditor文本编辑器-图文实例
1.什么是KindEditor KindEditor 是一套开源的在线HTML编辑器,主要用于让用户在网站上获得所见即所得编辑效果,开发人员可以用 KindEditor 把传统的多行文本输入框(tex ...
- 开源字体包缩减font-spider
最近公司产品用微软雅黑,被告~ 领导要求产品后台系统必须更换字体,美工给了个宋体,个人觉得太丑,就网上搜了些处理方法使用 CSS属性 @font-face 自定义字体, 最后决定用 思源黑体 (思源 ...
- linux服务端的网络编程
常见的Linux服务端的开发模型有多进程.多线程和IO复用,即select.poll和epoll三种方式,其中现在广泛使用的IO模型主要epoll,关于该模型的性能相较于select和poll要好不少 ...