1 题目

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]

2 分析和实现

用分治思想实现

class Solution {
public:vector<int> diffWaysToCompute(string input) {vector<int> res;for(int i = 0; i < input.size(); i++) {char cur = input[i];if(cur == '+' || cur == '-' || cur == '*') {vector<int> res_left = diffWaysToCompute(input.substr(0, i));vector<int> res_right = diffWaysToCompute(input.substr(i + 1));for(int x = 0; x < res_left.size(); x++) {for(int y = 0; y < res_right.size(); y++) {if(cur == '+')res.push_back(res_left[x] + res_right[y]);else if(cur == '-')res.push_back(res_left[x] - res_right[y]);else if(cur == '*')res.push_back(res_left[x] * res_right[y]);}}}}//单独一个数字时if(res.empty()) {res.push_back(atoi(input.c_str()));}return res;   }
};

LeetCode----241. Different Ways to Add Parenthese(M)分治相关推荐

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

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

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

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

  3. 241. Different Ways to Add Parentheses

    241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parenthes ...

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

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

  5. LeedCode 241. Different Ways to Add Parentheses

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

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

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

  7. 【重点!DP】LeetCode 639. Decode Ways II

    LeetCode 639. Decode Ways II 参考网址:https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-639-d ...

  8. 【DFS + 记忆化递归 + DP】LeetCode 91. Decode Ways

    LeetCode 91. Decode Ways Solution1:我的答案 还是记录一下,最容易想到的是DFS,但是在第223/238个case上就超时了... class Solution { ...

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

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

最新文章

  1. 平安京服务器维护不能打字,《决战!平安京》:玩的真的累,我真的是服了这破游戏的举报系统...
  2. 多个Google账户合并(共享)数据
  3. 4/100. Maximum Depth of Binary Tree
  4. memcpy(cv::Mat.data,unsigned char *,320x240),保存数据失真问题解决方法
  5. 【洛谷3157】[CQOI2011] 动态逆序对(CDQ分治)
  6. tiledmap 图块属性_TiledMap地图使用
  7. 铃声截取软件android6,铃声剪辑
  8. 标准误计算机excel公式,关于excel计算标准差SD和标准误SE的方法
  9. 汽车称重软件系统配置(一)
  10. R语言---ggplot图片边缘空白设置和ggplot映射的shape值多余6个
  11. Matlab中的mod()函数
  12. 3d打印打印头heating failure报错
  13. 小雷的冰茶几 3386
  14. Writeback和Writethrough区别
  15. Ubuntu18.04运行校园网客户端
  16. SolidWorks二次开发-关于录制宏保存的格式
  17. Greenplum集群部署和架构优化,我总结了5000字的心得
  18. Arduino远程无线通信
  19. 数据探索与数据预处理的实验报告
  20. Qgis 加载在线地图:如高德、天地图、OSM等

热门文章

  1. Matlab将二维矩阵三维显示——mesh的使用实例
  2. XilinxFPGA: Vivado ISE
  3. VPN服务器配置、路由服务器配置、视频服务器配置、DNS服务器配置、DHCP服务器配置、E-mail服务器配置、FTP服务器配置、Web服务器配置、证书服务器配置及管理
  4. vis.js网络拓扑图自定义图片
  5. 联想服务器改win7系统教程视频,联想台式电脑win10改win7系统教程
  6. Android显示GIF动画
  7. 多线程编程的目标与挑战
  8. 飘雪网吧专用XP系统 v7.0
  9. Python经典例题——超市买水果系统
  10. 第16届北京国际体育电影周正式启动