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. CentOS笔记——配置DNS服务器

    前话 咳咳,这次Linux系统的DNS服务器搭建我不得不记下来.,这错误真的太蛋疼了,我整整弄了两天才解决问题(抱歉我很蠢). 也许有人会和我犯同样的错误,给大家分享一下经验. 首先总结一下知识点: ...

  2. windows8.1下安装.NET Framework 3.5

    今天安装Arcgis10.2提示需要安装.NET Framework 3.5.校园网的网速,你懂的.所以,在线安装不太现实. 在线安装方法: 如何在 Windows 8 上安装 .NET Framew ...

  3. POJ1061——青蛙的约会(扩展欧几里德)

    青蛙的约会 Description两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件 ...

  4. 关于结构体的PPT

    http://wenku.baidu.com/link?url=bd75dv-I7pCuw9M7W8iFqu6jti6XPdUqVYDC1sXegmMpaacY7TPo-P9Vx38b8zBLj6Nd ...

  5. 查看32bit的ARM(比如ARMv7)反汇编

    1.使用./arm-eabi-as test.S -o test.o编译 2.使用./arm-eabi-objdump -d test.o反汇编

  6. Tiny4412之C语言实现流水灯,Tiny4412裸机程序[3]

    在前边我们使用汇编完成了一个流水灯实验: Tiny4412汇编流水灯代码,Tiny4412裸机LED操作 ---- - -- -- -- - -- -- 修改: # ${MKBL2} ${SOURCE ...

  7. 【JAVA】别特注意,POI中getLastRowNum() 和getLastCellNum()的区别

    hssfSheet.getLastRowNum();.行标,比行数小1 hssfSheet.getRow(k).getLastCellNum();//获取列数,比最后一列列标大1 行标.列标都以0开始 ...

  8. bzoj1412

    比较裸的最小割 注意狼和羊的领地可以通过空地相连 ;       dx:..] ,,,-);       dy:..] ,,,); type node=record        next,point ...

  9. 关于Azure存储账户中存储虚拟机VHD文件的注意事项

     Joy Qiao from MSFT  Thu, Mar 12 2015 3:16 PM 我们在使用Azure时经常都会在Azure存储账户中放一些文件,包括Azure虚机的VHD文件也都是放在存储 ...

  10. gulp入门

    gulp是基于Nodejs的自动化任务工具,类似java中的ant,结合相关插件可方便的完成javascript/coffee/sass/less/html/image/css等文件的的测试.检查.合 ...