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

  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. [LeetCode#241]Different Ways to Add Parentheses

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

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

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

  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. uva 10154

    dp  记忆化搜索 做的时候像dfs  #include <iostream> #include <cstring> #include <cstdio> #incl ...

  2. ural 1123

    找大于等于原数的最小回文数字  代码比较烂........... #include <iostream> #include <cstdio> #include <cstr ...

  3. Linux资源监控_Nmon

    性能测试中,各个服务器资源占用统计分析是一个很重要的组成部分,通常我们使用nmon这个工具来进行监控以及监控结果输出. 一. 在监控阶段使用类似下面的命令 ./nmon -f write_3s_20v ...

  4. 李洪强iOS开发之使用 Reachability 检测网络

    1.iOS平台是按照一直有网络连接的思路来设计的,开发者利用这一特点创造了很多优秀的第三方应用. 大多数的iOS应用都需要联网,甚至有些应用严重依赖网络,没有网络就无法正常工作. 2.在你的应用尝试通 ...

  5. Servlet课程0426(九)Servlet服务器端创建Cookie和客户端读取Cookie

    服务器端创建Cookie: Win7默认Cookie位置 C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Cookies Cookie ...

  6. ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map

    原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...

  7. POJ1265——Area(Pick定理+多边形面积)

    Area DescriptionBeing well known for its highly innovative products, Merck would definitely be a goo ...

  8. R语言学习笔记:怎么从txt中读入数据

    1   从该链接中下载测试数据,http://pan.baidu.com/share/link?shareid=3322971616&uk=3862050759   2   把测试文件Anal ...

  9. python学习笔记六--用户自定义类

    一.类: 1. 面向对象. 2. 定义了新的对象类型. 定义了两个属性:name,pay 定义了两个方法:lastName,giveRaise

  10. poj1226,poj3080

    看来以后用pascal的函数要小心了: 简简单单pos其实时间复杂度是二次方级的…… 今天学习的是KMP——字符匹配算法: 这两道题也都很简单,都是为这个算法练手的, 最朴素的匹配显然是穷举起始位置然 ...