Skip to content

Commit 3dac8b9

Browse files
authored
Merge pull request halfrost#143 from novahe/fix/150
clean up redundant code
2 parents 9fb3914 + 5cd4324 commit 3dac8b9

File tree

2 files changed

+34
-80
lines changed

2 files changed

+34
-80
lines changed

leetcode/0150.Evaluate-Reverse-Polish-Notation/150. Evaluate Reverse Polish Notation.go

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,23 @@ import (
55
)
66

77
func evalRPN(tokens []string) int {
8-
if len(tokens) == 1 {
9-
i, _ := strconv.Atoi(tokens[0])
10-
return i
11-
}
12-
stack, top := []int{}, 0
13-
for _, v := range tokens {
14-
switch v {
15-
case "+":
16-
{
17-
sum := stack[top-2] + stack[top-1]
18-
stack = stack[:top-2]
19-
stack = append(stack, sum)
20-
top--
21-
}
22-
case "-":
23-
{
24-
sub := stack[top-2] - stack[top-1]
25-
stack = stack[:top-2]
26-
stack = append(stack, sub)
27-
top--
28-
}
29-
case "*":
30-
{
31-
mul := stack[top-2] * stack[top-1]
32-
stack = stack[:top-2]
33-
stack = append(stack, mul)
34-
top--
35-
}
36-
case "/":
37-
{
38-
div := stack[top-2] / stack[top-1]
39-
stack = stack[:top-2]
40-
stack = append(stack, div)
41-
top--
42-
}
43-
default:
44-
{
45-
i, _ := strconv.Atoi(v)
46-
stack = append(stack, i)
47-
top++
8+
stack := make([]int, 0, len(tokens))
9+
for _, token := range tokens {
10+
v, err := strconv.Atoi(token)
11+
if err == nil {
12+
stack = append(stack, v)
13+
} else {
14+
num1, num2 := stack[len(stack)-2], stack[len(stack)-1]
15+
stack = stack[:len(stack)-2]
16+
switch token {
17+
case "+":
18+
stack = append(stack, num1+num2)
19+
case "-":
20+
stack = append(stack, num1-num2)
21+
case "*":
22+
stack = append(stack, num1*num2)
23+
case "/":
24+
stack = append(stack, num1/num2)
4825
}
4926
}
5027
}

website/content/ChapterFour/0100~0199/0150.Evaluate-Reverse-Polish-Notation.md

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,46 +66,23 @@ import (
6666
)
6767

6868
func evalRPN(tokens []string) int {
69-
if len(tokens) == 1 {
70-
i, _ := strconv.Atoi(tokens[0])
71-
return i
72-
}
73-
stack, top := []int{}, 0
74-
for _, v := range tokens {
75-
switch v {
76-
case "+":
77-
{
78-
sum := stack[top-2] + stack[top-1]
79-
stack = stack[:top-2]
80-
stack = append(stack, sum)
81-
top--
82-
}
83-
case "-":
84-
{
85-
sub := stack[top-2] - stack[top-1]
86-
stack = stack[:top-2]
87-
stack = append(stack, sub)
88-
top--
89-
}
90-
case "*":
91-
{
92-
mul := stack[top-2] * stack[top-1]
93-
stack = stack[:top-2]
94-
stack = append(stack, mul)
95-
top--
96-
}
97-
case "/":
98-
{
99-
div := stack[top-2] / stack[top-1]
100-
stack = stack[:top-2]
101-
stack = append(stack, div)
102-
top--
103-
}
104-
default:
105-
{
106-
i, _ := strconv.Atoi(v)
107-
stack = append(stack, i)
108-
top++
69+
stack := make([]int, 0, len(tokens))
70+
for _, token := range tokens {
71+
v, err := strconv.Atoi(token)
72+
if err == nil {
73+
stack = append(stack, v)
74+
} else {
75+
num1, num2 := stack[len(stack)-2], stack[len(stack)-1]
76+
stack = stack[:len(stack)-2]
77+
switch token {
78+
case "+":
79+
stack = append(stack, num1+num2)
80+
case "-":
81+
stack = append(stack, num1-num2)
82+
case "*":
83+
stack = append(stack, num1*num2)
84+
case "/":
85+
stack = append(stack, num1/num2)
10986
}
11087
}
11188
}

0 commit comments

Comments
 (0)