Skip to content

Commit 67ccbe6

Browse files
committed
feat: add solutions to lc problem: No.0241
No.0241.Different Ways to Add Parentheses
1 parent ed04b15 commit 67ccbe6

File tree

6 files changed

+394
-70
lines changed

6 files changed

+394
-70
lines changed

solution/0200-0299/0241.Different Ways to Add Parentheses/README.md

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,161 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:记忆化搜索**
53+
5254
<!-- tabs:start -->
5355

5456
### **Python3**
5557

5658
<!-- 这里可写当前语言的特殊实现逻辑 -->
5759

5860
```python
59-
61+
class Solution:
62+
def diffWaysToCompute(self, expression: str) -> List[int]:
63+
@cache
64+
def dfs(exp):
65+
if exp.isdigit():
66+
return [int(exp)]
67+
ans = []
68+
for i, c in enumerate(exp):
69+
if c in '-+*':
70+
left, right = dfs(exp[:i]), dfs(exp[i + 1:])
71+
for a in left:
72+
for b in right:
73+
if c == '-':
74+
ans.append(a - b)
75+
elif c == '+':
76+
ans.append(a + b)
77+
else:
78+
ans.append(a * b)
79+
return ans
80+
81+
return dfs(expression)
6082
```
6183

6284
### **Java**
6385

6486
<!-- 这里可写当前语言的特殊实现逻辑 -->
6587

6688
```java
89+
class Solution {
90+
private static Map<String, List<Integer>> memo = new HashMap<>();
91+
92+
public List<Integer> diffWaysToCompute(String expression) {
93+
return dfs(expression);
94+
}
95+
96+
private List<Integer> dfs(String exp) {
97+
if (memo.containsKey(exp)) {
98+
return memo.get(exp);
99+
}
100+
List<Integer> ans = new ArrayList<>();
101+
if (exp.length() < 3) {
102+
ans.add(Integer.parseInt(exp));
103+
return ans;
104+
}
105+
for (int i = 0; i < exp.length(); ++i) {
106+
char c = exp.charAt(i);
107+
if (c == '-' || c == '+' || c == '*') {
108+
List<Integer> left = dfs(exp.substring(0, i));
109+
List<Integer> right = dfs(exp.substring(i + 1));
110+
for (int a : left) {
111+
for (int b : right) {
112+
if (c == '-') {
113+
ans.add(a - b);
114+
} else if (c == '+') {
115+
ans.add(a + b);
116+
} else {
117+
ans.add(a * b);
118+
}
119+
}
120+
}
121+
}
122+
}
123+
memo.put(exp, ans);
124+
return ans;
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
class Solution {
133+
public:
134+
vector<int> diffWaysToCompute(string expression) {
135+
return dfs(expression);
136+
}
137+
138+
vector<int> dfs(string exp) {
139+
if (memo.count(exp)) return memo[exp];
140+
if (exp.size() < 3) return {stoi(exp)};
141+
vector<int> ans;
142+
int n = exp.size();
143+
for (int i = 0; i < n; ++i)
144+
{
145+
char c = exp[i];
146+
if (c == '-' || c == '+' || c == '*')
147+
{
148+
vector<int> left = dfs(exp.substr(0, i));
149+
vector<int> right = dfs(exp.substr(i + 1, n - i - 1));
150+
for (int& a : left)
151+
{
152+
for (int& b : right)
153+
{
154+
if (c == '-') ans.push_back(a - b);
155+
else if (c == '+') ans.push_back(a + b);
156+
else ans.push_back(a * b);
157+
}
158+
}
159+
}
160+
}
161+
memo[exp] = ans;
162+
return ans;
163+
}
164+
165+
private:
166+
unordered_map<string, vector<int>> memo;
167+
};
168+
```
67169

170+
### **Go**
171+
172+
```go
173+
var memo = map[string][]int{}
174+
175+
func diffWaysToCompute(expression string) []int {
176+
return dfs(expression)
177+
}
178+
179+
func dfs(exp string) []int {
180+
if v, ok := memo[exp]; ok {
181+
return v
182+
}
183+
if len(exp) < 3 {
184+
v, _ := strconv.Atoi(exp)
185+
return []int{v}
186+
}
187+
ans := []int{}
188+
for i, c := range exp {
189+
if c == '-' || c == '+' || c == '*' {
190+
left, right := dfs(exp[:i]), dfs(exp[i+1:])
191+
for _, a := range left {
192+
for _, b := range right {
193+
if c == '-' {
194+
ans = append(ans, a-b)
195+
} else if c == '+' {
196+
ans = append(ans, a+b)
197+
} else {
198+
ans = append(ans, a*b)
199+
}
200+
}
201+
}
202+
}
203+
}
204+
memo[exp] = ans
205+
return ans
206+
}
68207
```
69208

70209
### **...**

solution/0200-0299/0241.Different Ways to Add Parentheses/README_EN.md

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,150 @@
4848
### **Python3**
4949

5050
```python
51-
51+
class Solution:
52+
def diffWaysToCompute(self, expression: str) -> List[int]:
53+
@cache
54+
def dfs(exp):
55+
if exp.isdigit():
56+
return [int(exp)]
57+
ans = []
58+
for i, c in enumerate(exp):
59+
if c in '-+*':
60+
left, right = dfs(exp[:i]), dfs(exp[i + 1:])
61+
for a in left:
62+
for b in right:
63+
if c == '-':
64+
ans.append(a - b)
65+
elif c == '+':
66+
ans.append(a + b)
67+
else:
68+
ans.append(a * b)
69+
return ans
70+
71+
return dfs(expression)
5272
```
5373

5474
### **Java**
5575

5676
```java
77+
class Solution {
78+
private static Map<String, List<Integer>> memo = new HashMap<>();
79+
80+
public List<Integer> diffWaysToCompute(String expression) {
81+
return dfs(expression);
82+
}
83+
84+
private List<Integer> dfs(String exp) {
85+
if (memo.containsKey(exp)) {
86+
return memo.get(exp);
87+
}
88+
List<Integer> ans = new ArrayList<>();
89+
if (exp.length() < 3) {
90+
ans.add(Integer.parseInt(exp));
91+
return ans;
92+
}
93+
for (int i = 0; i < exp.length(); ++i) {
94+
char c = exp.charAt(i);
95+
if (c == '-' || c == '+' || c == '*') {
96+
List<Integer> left = dfs(exp.substring(0, i));
97+
List<Integer> right = dfs(exp.substring(i + 1));
98+
for (int a : left) {
99+
for (int b : right) {
100+
if (c == '-') {
101+
ans.add(a - b);
102+
} else if (c == '+') {
103+
ans.add(a + b);
104+
} else {
105+
ans.add(a * b);
106+
}
107+
}
108+
}
109+
}
110+
}
111+
memo.put(exp, ans);
112+
return ans;
113+
}
114+
}
115+
```
116+
117+
### **C++**
118+
119+
```cpp
120+
class Solution {
121+
public:
122+
vector<int> diffWaysToCompute(string expression) {
123+
return dfs(expression);
124+
}
125+
126+
vector<int> dfs(string exp) {
127+
if (memo.count(exp)) return memo[exp];
128+
if (exp.size() < 3) return {stoi(exp)};
129+
vector<int> ans;
130+
int n = exp.size();
131+
for (int i = 0; i < n; ++i)
132+
{
133+
char c = exp[i];
134+
if (c == '-' || c == '+' || c == '*')
135+
{
136+
vector<int> left = dfs(exp.substr(0, i));
137+
vector<int> right = dfs(exp.substr(i + 1, n - i - 1));
138+
for (int& a : left)
139+
{
140+
for (int& b : right)
141+
{
142+
if (c == '-') ans.push_back(a - b);
143+
else if (c == '+') ans.push_back(a + b);
144+
else ans.push_back(a * b);
145+
}
146+
}
147+
}
148+
}
149+
memo[exp] = ans;
150+
return ans;
151+
}
152+
153+
private:
154+
unordered_map<string, vector<int>> memo;
155+
};
156+
```
57157

158+
### **Go**
159+
160+
```go
161+
var memo = map[string][]int{}
162+
163+
func diffWaysToCompute(expression string) []int {
164+
return dfs(expression)
165+
}
166+
167+
func dfs(exp string) []int {
168+
if v, ok := memo[exp]; ok {
169+
return v
170+
}
171+
if len(exp) < 3 {
172+
v, _ := strconv.Atoi(exp)
173+
return []int{v}
174+
}
175+
ans := []int{}
176+
for i, c := range exp {
177+
if c == '-' || c == '+' || c == '*' {
178+
left, right := dfs(exp[:i]), dfs(exp[i+1:])
179+
for _, a := range left {
180+
for _, b := range right {
181+
if c == '-' {
182+
ans = append(ans, a-b)
183+
} else if c == '+' {
184+
ans = append(ans, a+b)
185+
} else {
186+
ans = append(ans, a*b)
187+
}
188+
}
189+
}
190+
}
191+
}
192+
memo[exp] = ans
193+
return ans
194+
}
58195
```
59196

60197
### **...**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
vector<int> diffWaysToCompute(string expression) {
4+
return dfs(expression);
5+
}
6+
7+
vector<int> dfs(string exp) {
8+
if (memo.count(exp)) return memo[exp];
9+
if (exp.size() < 3) return {stoi(exp)};
10+
vector<int> ans;
11+
int n = exp.size();
12+
for (int i = 0; i < n; ++i)
13+
{
14+
char c = exp[i];
15+
if (c == '-' || c == '+' || c == '*')
16+
{
17+
vector<int> left = dfs(exp.substr(0, i));
18+
vector<int> right = dfs(exp.substr(i + 1, n - i - 1));
19+
for (int& a : left)
20+
{
21+
for (int& b : right)
22+
{
23+
if (c == '-') ans.push_back(a - b);
24+
else if (c == '+') ans.push_back(a + b);
25+
else ans.push_back(a * b);
26+
}
27+
}
28+
}
29+
}
30+
memo[exp] = ans;
31+
return ans;
32+
}
33+
34+
private:
35+
unordered_map<string, vector<int>> memo;
36+
};

0 commit comments

Comments
 (0)