leetcode 241. Different Ways to Add Parentheses 深度优先遍历DFS + 类似构造所有的二叉搜索树

蔚落 2022-06-08 12:49 256阅读 0赞

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]

这道题就是考察添加括号,最初的想法是中序转后序,然后判定优先级来做。

后来网上看到了一个超级棒的做法。

建议直接看C++的这做法,这道题十分的棒,直接递归即可

强烈建议和leetcode 95. Unique Binary Search Trees II 递归构造所有可能的搜索二叉树BST + 卡特兰数 和 leetcode 96. Unique Binary Search Trees 卡特兰数:BST的数量 + 栈出栈数量 一起学习,它们的做法很相识,值得放在一起学习

还有这一道题leetcode 282. Expression Add Operators 任意添加运算符计算结果 +深度优先遍历DFS

代码如下:

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. /*
  4. * 亏我还一直想着中序转后序,然后判定不同的优先级去做计算
  5. * 这个题的思路就是针对每一个运算符去递归去做计算
  6. *
  7. * 首先是基本的递归的思想,从前往后遍历,
  8. * 对于每一个运算符将其左右子字符串分成两部分,
  9. * 然后计算两边的各种不同的结果来,然后和在一起,
  10. * 这中间不存是不存在加括号的相同发的情况的。
  11. *
  12. * */
  13. public class Solution
  14. {
  15. public List<Integer> diffWaysToCompute(String input)
  16. {
  17. List<Integer> res=new ArrayList<Integer>();
  18. for(int i=0;i<input.length();i++)
  19. {
  20. char one=input.charAt(i);
  21. if(one=='-' || one=='+' || one=='*')
  22. {
  23. List<Integer> left = diffWaysToCompute(input.substring(0,i));
  24. List<Integer> right = diffWaysToCompute(input.substring(i+1));
  25. for(int k=0;k<left.size();k++)
  26. {
  27. for(int j=0;j<right.size();j++)
  28. {
  29. if(one=='-')
  30. res.add(left.get(k)-right.get(j));
  31. else if(one=='+')
  32. res.add(left.get(k)+right.get(j));
  33. else
  34. res.add(left.get(k)*right.get(j));
  35. }
  36. }
  37. }
  38. }
  39. if(res.isEmpty())
  40. res.add(Integer.parseInt(input));
  41. return res;
  42. }
  43. }

下面是C++的做法,就是做一个DFS遍历,这道题其实很难想到这么做。所以很值得学习

代码如下:

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <unordered_map>
  5. #include <set>
  6. #include <unordered_set>
  7. #include <queue>
  8. #include <stack>
  9. #include <string>
  10. #include <climits>
  11. #include <algorithm>
  12. #include <sstream>
  13. #include <functional>
  14. #include <bitset>
  15. #include <numeric>
  16. #include <cmath>
  17. #include <regex>
  18. using namespace std;
  19. class Solution
  20. {
  21. public:
  22. vector<int> diffWaysToCompute(string input)
  23. {
  24. vector<int> res;
  25. if (input.length()<=0 || isNum(input) == true)
  26. res.push_back(stoi(input));
  27. else
  28. {
  29. for (int i = 0; i < input.length(); i++)
  30. {
  31. char c = input[i];
  32. if (c == '+' || c == '-' || c == '*')
  33. {
  34. vector<int> left = diffWaysToCompute(input.substr(0, i));
  35. vector<int> right = diffWaysToCompute(input.substr(i+1));
  36. for (int l : left)
  37. {
  38. for (int r : right)
  39. {
  40. if (c == '+')
  41. res.push_back(l + r);
  42. else if (c == '-')
  43. res.push_back(l - r);
  44. else if (c == '*')
  45. res.push_back(l * r);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. return res;
  52. }
  53. bool isNum(string s)
  54. {
  55. for (char c : s)
  56. {
  57. if (c < '0' || c > '9')
  58. return false;
  59. }
  60. return true;
  61. }
  62. };

发表评论

表情:
评论列表 (有 0 条评论,256人围观)

还没有评论,来说两句吧...

相关阅读