Skip to content

Commit 148e827

Browse files
committed
0241 solved.
1 parent dce82fb commit 148e827

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0241)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0241 main.cpp)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/// Source : https://leetcode.com/problems/different-ways-to-add-parentheses/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-30
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Brute Force
12+
/// Time Complexity: O(n!)
13+
/// Space Complexity: O(n)
14+
class Solution {
15+
public:
16+
vector<int> diffWaysToCompute(string exp) {
17+
18+
vector<int> nums;
19+
vector<char> ops;
20+
for(int start = 0, i = 1; i <= exp.size(); i ++)
21+
if(i == exp.size() || !isdigit(exp[i])){
22+
nums.push_back(atoi(exp.substr(start, i - start).c_str()));
23+
24+
if(i < exp.size()) ops.push_back(exp[i]);
25+
26+
start = i + 1;
27+
i = start;
28+
}
29+
30+
// for(int e: nums) cout << e << ' '; cout << '\n';
31+
// for(char op: ops) cout << op << ' '; cout << '\n';
32+
33+
vector<int> res = dfs(nums, ops, 0, nums.size() - 1);
34+
return res;
35+
}
36+
37+
private:
38+
vector<int> dfs(const vector<int>& nums, const vector<char>& ops, int l, int r){
39+
40+
if(l == r) return {nums[l]};
41+
42+
vector<int> res;
43+
for(int left_end = l; left_end < r; left_end ++){
44+
vector<int> a = dfs(nums, ops, l, left_end);
45+
vector<int> b = dfs(nums, ops, left_end + 1, r);
46+
47+
for(int e1: a)
48+
for(int e2: b)
49+
res.push_back(calc(e1, e2, ops[left_end]));
50+
}
51+
return res;
52+
}
53+
54+
int calc(int a, int b, char op){
55+
if(op == '+') return a + b;
56+
if(op == '-') return a - b;
57+
if(op == '*') return a * b;
58+
59+
assert(false);
60+
return -1;
61+
}
62+
};
63+
64+
65+
void print_vec(const vector<int>& v){
66+
for(int e: v) cout << e << ' '; cout << '\n';
67+
}
68+
69+
int main() {
70+
71+
print_vec(Solution().diffWaysToCompute("2-1-1"));
72+
73+
print_vec(Solution().diffWaysToCompute("2*3-4*5"));
74+
75+
return 0;
76+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
280280
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [solution](https://leetcode.com/problems/product-of-array-except-self/solution/) | [C++](0001-0500/0238-Product-of-Array-Except-Self/cpp-0238/) | | [Python](0001-0500/0238-Product-of-Array-Except-Self/py-0238/) |
281281
| 239 | [Sliding Window Maximum](https://leetcode.com/problems/sliding-window-maximum/description/) | [solution](https://leetcode.com/problems/sliding-window-maximum/solution/) | [C++](0001-0500/0239-Sliding-Window-Maximum/cpp-0239/) | | |
282282
| 240 | [Search a 2D Matrix II](https://leetcode.com/problems/search-a-2d-matrix-ii/) | [solution](https://leetcode.com/problems/search-a-2d-matrix-ii/solution/) <br/> [缺:分治] | [C++](0001-0500/240-Search-a-2D-Matrix-II/cpp-240/) | | |
283-
| | | | | | |
283+
| 241 | [Different Ways to Add Parentheses](https://leetcode.com/problems/different-ways-to-add-parentheses/) | [] | [C++](0001-0500/241-Different-Ways-to-Add-Parentheses/cpp-241/) | | |
284284
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/description/) | [solution](https://leetcode.com/problems/valid-anagram/solution/) | [C++](0001-0500/0242-Valid-Anagram/cpp-0242/) | | |
285285
| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [solution](https://leetcode.com/problems/shortest-word-distance/solution/) | [C++](0001-0500/0243-Shortest-Word-Distance/cpp-0243/) | | |
286286
| 244 | [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/) | [solution](https://leetcode.com/problems/shortest-word-distance-ii/solution/) | [C++](0001-0500/0244-Shortest-Word-Distance-II/cpp-0244/) | | |

0 commit comments

Comments
 (0)