From 48de49930b709e4bd520868413236c2e84f0d152 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Wed, 9 Aug 2023 00:20:09 +0800 Subject: [PATCH] Add solution and test-cases for problem 241 --- .../Solution.go | 93 +++++++++++++++++++ .../Solution_test.go | 25 ++++- 2 files changed, 114 insertions(+), 4 deletions(-) diff --git a/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution.go b/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution.go index cfc60fd35..5b3c16b41 100644 --- a/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution.go +++ b/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution.go @@ -53,3 +53,96 @@ func ways(input string, cache map[string][]int) []int { return ans } + +// Solution2 +type operation struct { + val int + op byte +} + +func calResult(a, b int, op byte) int { + if op == '*' { + return a * b + } + if op == '+' { + return a + b + } + return a - b +} + +func diffWaysToCompute2(expression string) []int { + opts := make([]operation, 0) + base := 1 + c := 0 + // 245 + for idx := len(expression) - 1; idx >= 0; idx-- { + if expression[idx] >= '0' && expression[idx] <= '9' { + c = base*int(expression[idx]-'0') + c + base *= 10 + continue + } + opts = append(opts, operation{val: c}, operation{op: expression[idx]}) + base, c = 1, 0 + } + opts = append(opts, operation{val: c}) + + length := len(opts) + if length == 1 { + return []int{opts[0].val} + } + for s, e := 0, length-1; s < e; s, e = s+1, e-1 { + opts[s], opts[e] = opts[e], opts[s] + } + + ans := make([]int, 0) + dp := make([][]map[int]int, length) + // 需要明确dp[i][j] 能计算出多少种结果 + for i := 0; i < length; i++ { + dp[i] = make([]map[int]int, length) + for j := 0; j < length; j++ { + dp[i][j] = make(map[int]int) + } + if i&1 == 0 { + dp[i][i][opts[i].val] = 1 + } + } + + // 2 - 1 - 1-1 + // n=2 0-2, 2-4, 4-6 + // n=3,0-4, 2-6 + // n=4,0-6 + // n = 4 end=6 + // length=7 + // 2-1 + // 2 有一个操作数,3有两个,4有3个 + // 2 = 1+0 + // 3 = 2+1 + // 4 = 3 + 2 + // length - 1 - n+1 -n+2 + + for n := 2; n <= length/2+1; n++ { + for start := 0; start < length+2-2*n; start += 2 { + end := start + 2*(n-1) + for k := start; k < end; k += 2 { + for v1, c1 := range dp[start][k] { + for v2, c2 := range dp[k+2][end] { + r := calResult(v1, v2, opts[k+1].op) + dp[start][end][r] += c1 * c2 + } + } + } + } + } + for idx := length - 1; idx > 0; idx -= 2 { + op := opts[idx-1].op + for v1, c1 := range dp[idx][length-1] { + for v2, c2 := range dp[0][idx-2] { + r := calResult(v2, v1, op) + for loop := c1 * c2; loop > 0; loop-- { + ans = append(ans, r) + } + } + } + } + return ans +} diff --git a/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution_test.go b/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution_test.go index f0df87d76..e942286e8 100644 --- a/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution_test.go +++ b/leetcode/201-300/0241.Different-Ways-to-Add-Parentheses/Solution_test.go @@ -1,6 +1,7 @@ package Solution import ( + "sort" "strconv" "testing" ) @@ -12,8 +13,9 @@ func TestSolution(t *testing.T) { inputs string expect []int }{ - {"TestCase", "2-1-1", []int{0, 2}}, - {"TestCase", "2*3-4*5", []int{-34, -14, -10, -10, 10}}, + {"TestCase1", "2-1-1", []int{0, 2}}, + {"TestCase2", "2*3-4*5", []int{-34, -14, -10, -10, 10}}, + {"TestCase3", "10+5", []int{15}}, } // 开始测试 @@ -32,14 +34,29 @@ func TestSolution(t *testing.T) { t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs) } } + + got = diffWaysToCompute2(c.inputs) + sort.Ints(got) + if len(got) != len(c.expect) { + t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs) + } + m = make(map[int]int) + for v := range got { + m[v]++ + } + for v := range c.expect { + if _, ok := m[v]; !ok { + t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, got, c.inputs) + } + } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }