LeetCode解題報告 241. Different Ways to Add Parentheses [medium]
題目描述
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]
解題思路
題目意思就是說不考慮運算子優先順序之後,所有全排列組合的結果共有多少個。
詳細來說,就是從左到右遍歷字串,在遇到運算子之後,即對運算子左邊的字串和運算子右邊的字串分別求各自的所有可能情況,遞迴實現。
按照題目所給的example2的例子來說,就是分3次(3個運算子)
(1)*:(2*(3-(4*5))) = -34 (2*((3-4)*5)) = -10
左:2 右:3-4*5
繼續遞迴:
左:3
右:4*5
左:3-4
右:5
繼續遞迴:
3
4
4
5
(2)-:((2*3)-(4*5)) = -14
(3)*:((2*(3-4))*5) = -10 (((2*3)-4)*5) = 10
遞迴到最後都是單獨的數字(用atoi將字元轉換成int型),然後再按運算子倒推回去,即可得到結果。
時間複雜度分析
O(N3)的複雜度
程式碼如下:
class Solution {
public:
vector<int> diffWaysToCompute(string input) {
vector<int>result;
for (int i=0; i<input.size(); i++) {
if (input[i]=='+'||input[i]=='-'||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++) {
if (input[i]=='+'){
result.push_back(left[j]+right[k]);
}
else if (input[i]=='-'){
result.push_back(left[j]-right[k]);
}
else if(input[i]=='*'){
result.push_back(left[j]*right[k]);
}
}
}
}
}
if (result.empty())
result.push_back(atoi(input.c_str()));
return result;
}
};
相關文章
- LeetCode-Different Ways to Add ParenthesesLeetCode
- LeetCode解題報告 279. Perfect Squares [medium]LeetCode
- LeetCode解題報告 120. Triangle [medium]LeetCode
- LeetCode 解題報告 - 2. Add Two NumbersLeetCode
- LeetCode解題報告 452. Minimum Number of Arrows to Burst Balloons [medium]LeetCode
- LeetCode解題報告 108. Convert Sorted Array to Binary Search Tree [medium]LeetCode
- Leetcode 第136場周賽解題報告LeetCode
- LeetCode Weekly Contest 96 解題報告LeetCode
- Leetcode Weekly Contest 95解題報告LeetCode
- Leetcode Weekly Contest94 解題報告LeetCode
- [leetcode] 252. Meeting Rooms 解題報告LeetCodeOOM
- [Leetcode] 253. Meeting Rooms II 解題報告LeetCodeOOM
- Leetcode 22 Generate ParenthesesLeetCode
- Leetcode Valid ParenthesesLeetCode
- Leetcode-Generate ParenthesesLeetCode
- Generate Parentheses leetcode javaLeetCodeJava
- Leetcode-Decode WaysLeetCode
- Decode Ways leetcode javaLeetCodeJava
- Leetcode 20 Valid ParenthesesLeetCode
- leetcode22_Generate ParenthesesLeetCode
- Leetcode-Valid ParenthesesLeetCode
- Valid Parentheses leetcode javaLeetCodeJava
- leetcode-91-Decode WaysLeetCode
- Doris建立表報錯Failed to find enough host with storage medium問題解決AI
- 【LeetCode】253. Meeting Rooms II 解題報告(C++)LeetCodeOOMC++
- Leetcode 32 Longest Valid ParenthesesLeetCode
- LeetCode-Remove Invalid ParenthesesLeetCodeREM
- Leetcode-Longest Valid ParenthesesLeetCode
- [LeetCode] Generate Parentheses 生成括號LeetCode
- leetcode 之 Longest Valid ParenthesesLeetCode
- Longest Valid Parentheses leetcode javaLeetCodeJava
- [熵值] 解題報告熵
- 【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解題報告LeetCode
- [LeetCode] 32. Longest Valid ParenthesesLeetCode
- Leetcode - 022. Generate ParenthesesLeetCode
- leetcode32_Longest Valid ParenthesesLeetCode
- Leetcode-Medium 621. Task SchedulerLeetCode
- LeetCode - Medium - 322. Coin ChangeLeetCode