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 ...
随机推荐
- Python 开源异步并发框架的未来
http://segmentfault.com/a/1190000000471602 开源 Python 是开源的,介绍的这几个框架 Twisted.Tornado.Gevent 和 tulip 也都 ...
- Side-by-side assembly
Side-by-side technology is a standard for executable files in Windows 98 Second Edition, Windows 200 ...
- ECSHOP报错误Deprecated: preg_replace(): The /e modifier is depr
http://www.ecshoptemplate.com/article-1850.html
- SaaS系列介绍之四:我国SaaS市场发展
1 引言 那些没有经验的问题解决者们,几乎无一例外,都是去匆忙地寻找解决办法,而不是先给要解决的问题下定义. ...
- laravel Authentication and Security
Creating the user modelFirst of all, we need to define the model that is going to be used to represe ...
- FPGA中latency与delay概念的区别
2013-06-17 21:09:26 最近学习流水线以及状态机,总遇到注入加入寄存器可以分割组合逻辑,从而提高电路的运行频率的说法:还有流水线可以提高速度的说法,刚开始很是疑惑,觉得流水线的方法,输 ...
- Android 签名(2)签名知识要点
要点 1) 所有的应用程序都必须有数字证书,Android系统不会安装一个没有数字证书的应用程序 2) Android程序包使用的数字证书可以是自签名的,不需要一个权威的数字证书机构签名认证 3) 如 ...
- WinAPI——钩子函数大全
SetWindowsHookEx 函数功能:该函数将一个应用程序定义的挂钩处理过程安装到挂钩链中去,您可以通过安装挂钩处理过程来对系统的某些类型事件进行监控,这些事件与某个特定的线程或系统中的所有事件 ...
- C#反射机制介绍
反射的定义:审查元数据并收集关于它的类型信息的能力.元数据(编译以后的最基本数据单元)就是一大堆的表,当编译程序集或者模块时,编译器会创建一个类定义表,一个字段定义表,和一个方法定义表等. ...
- HTML5学习(五)----SVG
参考教程地址:http://www.w3school.com.cn/html5/html_5_svg.asp HTML5 支持内联 SVG. 什么是SVG? SVG 指可伸缩矢量图形 (Scalabl ...