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 = 0; i < input.size(); i++){if (isop(input[i])){vector<int> left = diffWaysToCompute(input.substr(0, i));vector<int> right = diffWaysToCompute(input.substr(i + 1));for (int j = 0; j < left.size(); j++){for (int k = 0; 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 = 0;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 0;
}

转载于:https://www.cnblogs.com/chess/p/5296502.html

241. Different Ways to Add Parentheses相关推荐

  1. leetcode 241. Different Ways to Add Parentheses | 241. 为运算表达式设计优先级(Java)

    题目 https://leetcode.com/problems/different-ways-to-add-parentheses/ 题解 参考:C++ Solution [Faster than ...

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

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

  3. LeedCode 241. Different Ways to Add Parentheses

    一.题目 Given a string expression of numbers and operators, return all possible results from computing ...

  4. LeetCode-241. Different Ways to Add Parentheses [C++][Java]

    LeetCode-241. Different Ways to Add Parentheseshttps://leetcode.com/problems/different-ways-to-add-p ...

  5. [LeetCode]241.Different Ways to Add Parenthese

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

  6. LeetCode Different Ways to Add Parentheses(递归,动态规划)

    问题:给出一个表达式,求其添加括号后可能的结果 思路:第一种方式是根据运算符号作分割,分为两个子问题. 第二种方式是在第一种方式下作记忆,避免重复计算问题. 第二种方式使用动态规划,先将操作数和操作符 ...

  7. LeetCode----241. Different Ways to Add Parenthese(M)分治

    1 题目 Given a string of numbers and operators, return all possible results from computing all the dif ...

  8. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  9. Leetcode重点250题

    LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...

最新文章

  1. 【数字信号处理】基本序列 ( 实指数序列 | 收敛序列 | 发散序列 )
  2. python3学习笔记(2)_list-tuple
  3. 云+X案例展 | 金融类:荣之联助力君康人寿构建新一代数据中心
  4. mysql table as_Mysql中create table as与like命令的区别
  5. 第 7 章 本地方法栈
  6. navicat for MySQL连接本地数据库时报1045错误的解决方法
  7. 项目管理相关的考试认证及证书价值介绍
  8. 《信号与系统学习笔记》—线性时不变系统(二)
  9. 双线性插值bilinear interpolation
  10. SPSS——统计描述
  11. 基于github上camera开源框架CameraKit,实现仿微信拍照摄像功能
  12. 代码审查工具 rietveld的安装与使用(二)
  13. 关于Holder不等式等号成立的条件的理解
  14. SUSE(Linux操作系统)
  15. 3626 三元一次方程(枚举)
  16. 与虎谋皮,饮鸩止渴,却有什么办法呢?
  17. 面部表情识别---学习笔记
  18. FME将用SHP数据对栅格影像数据进行裁剪
  19. Linux之文件共享
  20. 传奇私服游戏支付接口申请(已解决)

热门文章

  1. 截屏没有了_原来华为手机的截屏方法不止3种,用了这么久,现在才知道新玩法?...
  2. 深入Java核心 探秘Java垃圾回收机制
  3. Oracle中EXECUTE IMMEDIATE的使用技巧
  4. Oracle修改表列名与顺序的解决方案 (sql 修改列名)
  5. JavaEE5 Jax-ws 学习(Soap webservice)
  6. 一道百度前端线上笔试题
  7. 如何在移动端复制到剪切板
  8. Android 常用框架集合
  9. VirtualBox下Ubuntu利用桥接方式上网
  10. 当C++爬山壁纸——C++山寨版