241. Different Ways to Add Parentheses
241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parentheses/
思路就是:首先找到以运算符为根节点,分别计算左子串和右子串的所有结果的集合,然后依次进行组合计算。参考博客http://www.cnblogs.com/ganganloveu/p/4681439.html。
自己的思路错了,直接用两边只用了一个整数去接收左右子串的计算值!!
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int> temp;
for (int i = ; i < input.size(); i++){
if (isop(input[i])){
vector<int> left = diffWaysToCompute(input.substr(, i));
vector<int> right = diffWaysToCompute(input.substr(i + ));
for (int j = ; j < left.size(); j++){
for (int k = ; k < right.size(); k++){
temp.push_back(compute(left[j], right[k], input[i]));
}
}
}
}
if (temp.empty()){
temp.push_back(atoi(input.c_str()));
}
return temp;
} bool isop(char ch){
if (ch == '+' || ch == '-' || ch == '*')
return true;
return false;
} int compute(int v1, int v2, char ch){
int sum = ;
switch (ch){
case '+':
sum = v1 + v2; break;
case '-':
sum = v1 - v2; break;
case '*':
sum = v1*v2; break;
}
return sum;
} };
int main()
{
Solution test;
string te = "2+4*3";
vector<int> res=test.diffWaysToCompute(te);
for (auto it = res.begin(); it != res.end(); it++){
cout << *it << endl;
} return ;
}
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 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]* ...
- 【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 ...
- [LeetCode] 241. Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- 241. Different Ways to Add Parentheses——本质:DFS
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
Problem: Given a string of numbers and operators, return all possible results from computing all the ...
- 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 ...
随机推荐
- Elasticsearch mapping
//设置mapping Put: http://192.168.1.102:9200/indexName { "settings": { , }, "mappings&q ...
- js常用正则
var sTest="xxxkdsj234dogdog1234xx"var reTest1=/(dog){2}/var reTest2 = /(?:dog){2}/;console ...
- webView 点击页面跳转到浏览器
@interface ForumDetailViewController ()<UIWebViewDelegate> { NSUInteger _clickedNumber; } @end ...
- PTA Sort Three Distinct Keys
Suppose you have an array of N elements, containing three distinct keys, "true", "fal ...
- MVC将服务器端的物理路径转换为服务器路径
以图片为例 后台Controller.cs public FileResult ImageUrl(string file) { return File("物理路径"+file, & ...
- 启用与关闭 Ad Hoc Distributed Queries
在数据库里执行以下脚本: 启用: exec sp_configure 'show advanced options',1reconfigureexec sp_configure 'Ad Hoc Dis ...
- 记录容易忘记的知识点(html 内容)
<xx 表文件名> 导入外部样式表 <link type="text/css" rel="stylesheet" href="xx. ...
- mvc 4 ActionFilterAttribute 特性,进行权限验证
权限验证: /// <summary> /// 管理员身份验证 /// </summary> public class BasicAuthenticationAttribute ...
- js script中引用其他script
在需要引用目标js中引用其他js依赖项 可以使用这个方法直接在js顶部加入这一行即可 document.write("<script type='text/javascript' sr ...
- 约瑟夫环问题(c++)
#include <iostream> struct node{ int payload; node* next; node(int payload){this->payload = ...