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 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]
为数不多的考察分治算法的题:
class Solution {
public: bool isNumber(string s) {
for(int i=; i<s.length(); ++i) {
if(!(s[i] <= '' && s[i] >= '')) return false;
}
return true;
} int toInt(string s) {
if(s == "") return ; int res = ;
for(int i=; i<s.length(); ++i) {
res = res* + (s[i]-'');
}
return res;
} vector<int> dfs(string input) {
vector<int> load;
if(isNumber(input)) {
load.push_back(toInt(input));
return load;
} int len = input.length();
vector<int> l, r;
for(int i=; i<len; ++i) {
if(input[i] == '-') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] - r[q]);
}
}
}
else if(input[i] == '+') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] + r[q]);
}
}
}
else if(input[i] == '*') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
load.push_back(l[p] * r[q]);
}
}
}
} return load;
} vector<int> diffWaysToCompute(string input) {
vector<int> res;
int len = input.length();
if(len == ) return res;
if(isNumber(input)) {
res.push_back(toInt(input));
return res;
} vector<int> l, r;
for(int i=; i<len; ++i) {
if(input[i] == '-') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] - r[q]);
}
}
}
else if(input[i] == '+') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] + r[q]);
}
}
}
else if(input[i] == '*') {
l = dfs(input.substr(, i));
r = dfs(input.substr(i+, len-i-));
for(int p=; p<l.size(); ++p) {
for(int q=; q<r.size(); ++q) {
res.push_back(l[p] * r[q]);
}
}
}
} //sort(res.begin(), res.end());
return res;
}
};
leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)的更多相关文章
- 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 ...
- [LeetCode#241]Different Ways to Add Parentheses
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- (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为运算表达式设计优先级 (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 ...
随机推荐
- unity3d与eclipse集成开发android应用
原地址:http://blog.csdn.net/armoonwei/article/details/7032537 Unity as a Library Once you have eclipse ...
- POJ2349+prim
最小生成树 /* prim 题意:给定一些点,一些卫星,一个卫星能连接两个点,点和点之间通信有一定的距离限制. 问能使得所有的点联通的最小距离. */ #include<stdio.h> ...
- java jdbc dbcp连接SQL Server
使用到的jar: commons-collections-3.1.jar commons-dbcp-1.4.jar commons-pool-1.5.6.jar sqljdbc4.jar dbcp配置 ...
- yii2的安装
yii2也是依赖于composer, 就像laravel, 所以先安装composer, 如果安装不上composer可以看laravel安装的文章. 安装好composer之后安装一个插件 comp ...
- Trainning Guide, Data Structures, Example
最近在复习数据结构,发现这套题不错,题目质量好,覆盖广,Data Structures部分包括Example,以及简单,中等,难三个部分,这几天把Example的做完了, 摘要如下: 通过这几题让我复 ...
- POJ3259——Wormholes(Bellman-Ford+SPFA)
Wormholes DescriptionWhile exploring his many farms, Farmer John has discovered a number of amazing ...
- 最短路径算法之三——Bellman-Ford算法
Bellman-Ford算法 Dijkstra算法无法判断含负权边的图的最短路. 如果遇到负权,在没有负权回路存在时,即便有负权的边,也可以采用Bellman-Ford算法正确求出最短路径. PS:负 ...
- P114、面试题17:合并两个排序的链表
题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按照递增顺序的.struct ListNode{ int m_nKey; ListNode* m_p ...
- org.apache.http.ProtocolException: Target host is not specified
对于httpClient4.3访问指定页面,可以从下面的demo抽取方法使用. 注意:对于URL必须使用 http://开始,否则会有如下报错信息: Caused by: org.apache.htt ...
- jquery uploadify上传文件插件导致浏览器崩溃问题解决方法
自谷歌浏览器更新到(版本39.0.2171.99 )后,访问上传文件界面浏览器就崩溃了,而其他的浏览器不会出现问题. 出现这种问题的原因就是谷歌浏览器缓存问题,但将访问该jsp页面路径添加上时间戳后无 ...