-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path241.为运算表达式设计优先级.go
121 lines (115 loc) · 2.62 KB
/
241.为运算表达式设计优先级.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import (
"strconv"
"strings"
)
/*
* @lc app=leetcode.cn id=241 lang=golang
*
* [241] 为运算表达式设计优先级
*
* https://leetcode-cn.com/problems/different-ways-to-add-parentheses/description/
*
* algorithms
* Medium (67.49%)
* Likes: 48
* Dislikes: 0
* Total Accepted: 1.9K
* Total Submissions: 2.9K
* Testcase Example: '"2-1-1"'
*
* 给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果。你需要给出所有可能的组合的结果。有效的运算符号包含 +, - 以及
* * 。
*
* 示例 1:
*
* 输入: "2-1-1"
* 输出: [0, 2]
* 解释:
* ((2-1)-1) = 0
* (2-(1-1)) = 2
*
* 示例 2:
*
* 输入: "2*3-4*5"
* 输出: [-34, -14, -10, -10, 10]
* 解释:
* (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
*
*/
func diffWaysToCompute(input string) []int {
operatorCnt := operatorCount(input)
if operatorCnt == 0 {
val, _ := strconv.Atoi(input)
return []int{val}
}
if operatorCnt == 1 {
val := calculateValue(input)
return []int{val}
}
result := make([]int, 0)
for i := 0; i < len(input); i++ {
if input[i] == '+' {
lefts := diffWaysToCompute(input[:i])
rights := diffWaysToCompute(input[i+1:])
for _, left := range lefts {
for _, right := range rights {
result = append(result, left+right)
}
}
}
if input[i] == '-' {
lefts := diffWaysToCompute(input[:i])
rights := diffWaysToCompute(input[i+1:])
for _, left := range lefts {
for _, right := range rights {
result = append(result, left-right)
}
}
}
if input[i] == '*' {
lefts := diffWaysToCompute(input[:i])
rights := diffWaysToCompute(input[i+1:])
for _, left := range lefts {
for _, right := range rights {
result = append(result, left*right)
}
}
}
}
return result
}
func operatorCount(input string) int {
count := 0
for i := 0; i < len(input); i++ {
if input[i] == '+' || input[i] == '-' || input[i] == '*' {
count++
}
}
return count
}
func calculateValue(input string) int {
index := strings.IndexByte(input, '+')
if index == -1 {
index = strings.IndexByte(input, '-')
}
if index == -1 {
index = strings.IndexByte(input, '*')
}
if input[index] == '+' {
val1, _ := strconv.Atoi(input[:index])
val2, _ := strconv.Atoi(input[index+1:])
return val1 + val2
}
if input[index] == '-' {
val1, _ := strconv.Atoi(input[:index])
val2, _ := strconv.Atoi(input[index+1:])
return val1 - val2
}
val1, _ := strconv.Atoi(input[:index])
val2, _ := strconv.Atoi(input[index+1:])
return val1 * val2
}