【Leetcode】241. Different Ways to Add Parentheses
2016-09-17 23:28
531 查看
题目:
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]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:
这道题的题意是给一个带有数字和运算符的字符串,得到该算式所有可能的答案。
我的思路是使用分治算法。将每个字符串分成两半再通过递归计算,将左右算式可能得到的结果保存在两个向量中,再通过两个for循环加在一起得到所有可能的答案。
代码:
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]
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:
这道题的题意是给一个带有数字和运算符的字符串,得到该算式所有可能的答案。
我的思路是使用分治算法。将每个字符串分成两半再通过递归计算,将左右算式可能得到的结果保存在两个向量中,再通过两个for循环加在一起得到所有可能的答案。
代码:
class Solution { public: vector<int> diffWaysToCompute(string input) { vector<int> l,r; vector<int> result; //保存算式可能的所有答案 result.clear(); int tt; int tag = 0; //标记字符串中是否存在运算符 for(int i = 0;i<input.length();i++) { if(input[i] == '+'||input[i] == '*'||input[i] == '-') //将字符串分成两半通过递归计算 { tag = 1; string temp; int j; for(j = 0;j<i;j++) temp += input[j]; l = diffWaysToCompute(temp); temp.clear(); for(j = 0;j<input.length()-i-1;j++) temp += input[j+i+1]; r = diffWaysToCompute(temp); for(int p = 0;p<l.size();p++) { for(int q = 0;q<r.size();q++) { if(input[i] == '+') { tt = l[p]+r[q]; result.push_back(tt); } else if(input[i] == '-') { tt = l[p]-r[q]; result.push_back(tt); } else if(input[i] == '*') { tt = l[p]*r[q]; result.push_back(tt); } } } } } if(tag == 0) //该字符串中不存在运算符号 { int num = 0,temp; int j = 0; for(int i = input.length()-1;i>=0;i--) { temp = input[j]-'0'; num += temp*pow(10,i); j++; } result.push_back(num); } return result; } };
相关文章推荐
- [leetcode] 241. Different Ways to Add Parentheses
- LeetCode *** 241. Different Ways to Add Parentheses
- Leetcode 241. Different Ways to Add Parentheses (Medium) (cpp)
- LeetCode解题报告 241. Different Ways to Add Parentheses [medium]
- Leetcode——241. Different Ways to Add Parentheses
- leetcode -- 241. Different Ways to Add Parentheses 【分治法】
- LeetCode 241. Different Ways to Add Parentheses(加括号的不同方法)
- [leetcode] 241. Different Ways to Add Parentheses
- 【leetcode题解】【再做一遍】【47】【M】241. Different Ways to Add Parentheses
- leetcode 241. Different Ways to Add Parentheses (Python版)
- <LeetCode OJ> 241. Different Ways to Add Parentheses
- [LeetCode]241. Different Ways to Add Parentheses
- LeetCode 241. Different Ways to Add Parentheses
- leetcode_middle_34_241. Different Ways to Add Parentheses
- 【LeetCode】241. Different Ways to Add Parentheses
- LeetCode 241. Different Ways to Add Parentheses
- leetcode 241. Different Ways to Add Parentheses-分治算法
- [leetcode] 241. Different Ways to Add Parentheses
- [LeetCode]241. Different Ways to Add Parentheses
- 【LeetCode】241. Different Ways to Add Parentheses