Skip to content

Commit 48de499

Browse files
committed
Add solution and test-cases for problem 241
1 parent cb54981 commit 48de499

File tree

2 files changed

+114
-4
lines changed

2 files changed

+114
-4
lines changed

leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,96 @@ func ways(input string, cache map[string][]int) []int {
5353

5454
return ans
5555
}
56+
57+
// Solution2
58+
type operation struct {
59+
val int
60+
op byte
61+
}
62+
63+
func calResult(a, b int, op byte) int {
64+
if op == '*' {
65+
return a * b
66+
}
67+
if op == '+' {
68+
return a + b
69+
}
70+
return a - b
71+
}
72+
73+
func diffWaysToCompute2(expression string) []int {
74+
opts := make([]operation, 0)
75+
base := 1
76+
c := 0
77+
// 245
78+
for idx := len(expression) - 1; idx >= 0; idx-- {
79+
if expression[idx] >= '0' && expression[idx] <= '9' {
80+
c = base*int(expression[idx]-'0') + c
81+
base *= 10
82+
continue
83+
}
84+
opts = append(opts, operation{val: c}, operation{op: expression[idx]})
85+
base, c = 1, 0
86+
}
87+
opts = append(opts, operation{val: c})
88+
89+
length := len(opts)
90+
if length == 1 {
91+
return []int{opts[0].val}
92+
}
93+
for s, e := 0, length-1; s < e; s, e = s+1, e-1 {
94+
opts[s], opts[e] = opts[e], opts[s]
95+
}
96+
97+
ans := make([]int, 0)
98+
dp := make([][]map[int]int, length)
99+
// 需要明确dp[i][j] 能计算出多少种结果
100+
for i := 0; i < length; i++ {
101+
dp[i] = make([]map[int]int, length)
102+
for j := 0; j < length; j++ {
103+
dp[i][j] = make(map[int]int)
104+
}
105+
if i&1 == 0 {
106+
dp[i][i][opts[i].val] = 1
107+
}
108+
}
109+
110+
// 2 - 1 - 1-1
111+
// n=2 0-2, 2-4, 4-6
112+
// n=3,0-4, 2-6
113+
// n=4,0-6
114+
// n = 4 end=6
115+
// length=7
116+
// 2-1
117+
// 2 有一个操作数,3有两个,4有3个
118+
// 2 = 1+0
119+
// 3 = 2+1
120+
// 4 = 3 + 2
121+
// length - 1 - n+1 -n+2
122+
123+
for n := 2; n <= length/2+1; n++ {
124+
for start := 0; start < length+2-2*n; start += 2 {
125+
end := start + 2*(n-1)
126+
for k := start; k < end; k += 2 {
127+
for v1, c1 := range dp[start][k] {
128+
for v2, c2 := range dp[k+2][end] {
129+
r := calResult(v1, v2, opts[k+1].op)
130+
dp[start][end][r] += c1 * c2
131+
}
132+
}
133+
}
134+
}
135+
}
136+
for idx := length - 1; idx > 0; idx -= 2 {
137+
op := opts[idx-1].op
138+
for v1, c1 := range dp[idx][length-1] {
139+
for v2, c2 := range dp[0][idx-2] {
140+
r := calResult(v2, v1, op)
141+
for loop := c1 * c2; loop > 0; loop-- {
142+
ans = append(ans, r)
143+
}
144+
}
145+
}
146+
}
147+
return ans
148+
}

leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package Solution
22

33
import (
4+
"sort"
45
"strconv"
56
"testing"
67
)
@@ -12,8 +13,9 @@ func TestSolution(t *testing.T) {
1213
inputs string
1314
expect []int
1415
}{
15-
{"TestCase", "2-1-1", []int{0, 2}},
16-
{"TestCase", "2*3-4*5", []int{-34, -14, -10, -10, 10}},
16+
{"TestCase1", "2-1-1", []int{0, 2}},
17+
{"TestCase2", "2*3-4*5", []int{-34, -14, -10, -10, 10}},
18+
{"TestCase3", "10+5", []int{15}},
1719
}
1820

1921
// 开始测试
@@ -32,14 +34,29 @@ func TestSolution(t *testing.T) {
3234
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
3335
}
3436
}
37+
38+
got = diffWaysToCompute2(c.inputs)
39+
sort.Ints(got)
40+
if len(got) != len(c.expect) {
41+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
42+
}
43+
m = make(map[int]int)
44+
for v := range got {
45+
m[v]++
46+
}
47+
for v := range c.expect {
48+
if _, ok := m[v]; !ok {
49+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs)
50+
}
51+
}
3552
})
3653
}
3754
}
3855

39-
//压力测试
56+
// 压力测试
4057
func BenchmarkSolution(b *testing.B) {
4158
}
4259

43-
//使用案列
60+
// 使用案列
4461
func ExampleSolution() {
4562
}

0 commit comments

Comments
 (0)