一、题目

Given a string expression of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. You may return the answer in any order.Example 1:Input: expression = "2-1-1"Output: [0,2]Explanation:
((2-1)-1) = 0
(2-(1-1)) = 2Example 2:Input: expression = "2*3-4*5"Output: [-34,-14,-10,-10,10]Explanation:
(2*(3-(4*5))) = -34
((2*3)-(4*5)) = -14
((2*(3-4))*5) = -10
(2*((3-4)*5)) = -10
(((2*3)-4)*5) = 10Constraints:1 <= expression.length <= 20expression consists of digits and the operator '+', '-', and '*'.All the integer values in the input expression are in the range [0, 99].

二、思路

  • 首先题目叫求添加括号来计算结果,可以有重复值,需要将所有可以添加括号的可能遍历
  • 可以分解为子问题,遇到一个符号+,−,∗+,-,*+,−,∗那么计算左边字符串可能的取值右边字符串可能的取值,然后合并得出当前字符串可能的取值
  • 对于一个字符串需要遍历所有的运算符号来获得所有的情况
  • 递归出口:当一个字符串没有符号时即全部为数字,那么只有一种取值即字符串本身

三、代码

class Solution {public:vector<int> diffWaysToCompute(string expression) {vector<int> ans;for (int i = 0; i < expression.size(); i++) {if (!(expression[i] >= '0' && expression[i] <= '9')) {//切割为左右vector<int> l = diffWaysToCompute(expression.substr(0, i));vector<int> r = diffWaysToCompute(expression.substr(i + 1, expression.size()));//将左右的结果保存在ansfor (int j = 0; j < l.size(); j++) {for (int k = 0; k < r.size(); k++) {switch(expression[i]) {case '+':ans.push_back(l[j] + r[k]);break;case '-':ans.push_back(l[j] - r[k]);break;case '*':ans.push_back(l[j] * r[k]);break;}}}}}   if (ans.size() == 0) {//递归出口代表只有一个数字 这时候直接将数字进行转化ans.push_back(stoi(expression));}return ans;}
};

LeedCode 241. Different Ways to Add Parentheses相关推荐

  1. 241. Different Ways to Add Parentheses

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

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

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

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

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

  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. PHP如何设置页码间距,PHP如何实现对work文档操作,返回文档的总页码数量
  2. python【力扣LeetCode算法题库】20- 有效的括号(辅助栈)
  3. java web 框架整合开发_SpringBoot(二)Web整合开发
  4. pandas - 案例(股票分析)
  5. ios 通过kvc修改属性会触发kvo_iOS开发-KVC和KVO的理解
  6. Azure Services Platform
  7. case when 效率高不高_扬州效率高的拍摄抖音短视频平台
  8. HTML 5.1 的 14 个新特性(含使用案例)
  9. wepy 父调用子组件方法_wepy踩坑小记(一)
  10. STM32 KEIL 下的 printf 函数
  11. Chrome 76.0.3809.100(正式版本) (64 位) 版本 77.0.3865.90不显示网址中的www怎么解决...
  12. 高斯计传感器使用的霍尔传感器原理
  13. pycharm下载安装opencv
  14. 最全解读】各种金融机构的产品分析(银行、证券、基金、信托...)
  15. 如何实现一个地图库封装,可以快速切换地图
  16. Java学习day078 Swing用户界面组件(四:选择组件)(复选框、单选钮、边框、组合框、滑动条)
  17. winform 如何让 comboBox1 不能输入,只能下拉选择
  18. ghost网络克隆功能实现【批量】计算机操作【系统的安装】,Ghost怎么网络克隆备份还原系统GhostCast教程...
  19. IC验证工程师高效战斗手册---提问之道(上篇)
  20. 下雨天客流量少,美容院怎么做生意

热门文章

  1. 英雄联盟(LOL)外挂原理(仅供学术交流)
  2. BigBrother的大数据之旅Day 10 hive(1)
  3. 如何查看win 10 无线密码
  4. 分别解释final,finally,finalize是什么?
  5. 与其道听途说,不如少走点弯路,轻松跟我来全面剖析 darknet 官网!
  6. 计算机分子模拟聚乙烯,高分子物理虚拟实验讲义
  7. 显卡的指标有哪些方面_纯干货!显卡购买重要参数:老司机勿入
  8. for循环不执行的小坑
  9. 辐射76 服务器位置,《辐射76》确认没有NPC 但将开放私人服务器与MOD
  10. android 调用oracle,Android 调用WCF实例详解