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

  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 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]* ...

  3. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  4. LC 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 添加括号的不同方式

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

  6. 241. Different Ways to Add Parentheses——本质:DFS

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

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

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

  8. [LeetCode#241]Different Ways to Add Parentheses

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

  9. 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 ...

随机推荐

  1. navDemo

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

  2. nullcon HackIM 2016 -- Crypto Question 4

    He is influential, he is powerful. He is your next contact you can get you out of this situation. Yo ...

  3. 手动安装m4, autoconf, automake, libtool

    转自http://ruby-china.org/topics/2434 系列文章原载于自己的博客,TOPI.CO (http://topi.co) ,某天不小心就push错啦,懒得从头再来,上传到Ru ...

  4. Weblogic常见故障常:JDBC Connection Pools(转)

    WebLogic Server中数据库连接池是一个经常出问题的地方,总结一下出问题的原因和解决办法. 一.数据库连接泄漏 此类问题一般都是由于开发人员没有正确关闭数据库连接造成的.比如使用完Conne ...

  5. oracle java SE

    http://www.oracle.com/technetwork/java/javase/downloads/index.html

  6. Linux 中 Vi 编辑器的简单操作

    Linux 中 Vi 编辑器的简单操作 Vi 编辑器一共有3种模式:命名模式(默认),尾行模式,编辑模式.3种模式彼此需要切换. 一.进入 Vi 编辑器的的命令 vi  filename //打开或新 ...

  7. C# Winform学习--- 实现石头剪刀布的游戏

    本文使用winform实现简单的石头剪刀布的游戏,主要实现,电脑随机出拳,玩家手动点击出拳:实现简易背景图片3秒切换:简易统计信息. 1.效果图 2.实现代码 新建一个windows窗体程序,用数字1 ...

  8. Cad 二次开发关于SelectCrossingPolygon和SelectFence返回结果Status为error的小测试

    CAD2008的二次开发,有个很奇怪的现象,只要你选择的点集不在当前视图上SelectCrossingPolygon和SelectFence返回结果Status就会为error,所以要获取正确的结果, ...

  9. hdu 2586 How far away ?(离线求最近公共祖先)

    #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i ...

  10. WCF权限控制

       前面写了 WCF账户密码认证, 实现了帐号密码认证, 接下来看看如何对方法的细粒度控制, 本文很大程度参考了 WCF安全之基于自定义声明授权策略, 这篇文章对原理讲得比较清楚, 而我这篇文章呢, ...