Skip to content

Commit fa467e6

Browse files
committed
feat: add solutions to lcof2 problem: No.036 Evaluate Reverse Polish Notation
1 parent f3663ff commit fa467e6

File tree

5 files changed

+203
-1
lines changed

5 files changed

+203
-1
lines changed

lcof2/剑指 Offer II 036. 后缀表达式/README.md

+108-1
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,129 @@
8686

8787
<!-- 这里可写通用的实现逻辑 -->
8888

89+
利用栈存储运算数,每次遇到符号,对栈顶两个元素进行运算
90+
8991
<!-- tabs:start -->
9092

9193
### **Python3**
9294

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

95-
```python
97+
需要注意 Python 的整除对负数也是向下取整(例如:`6 // -132 = -1`),和答案对应不上,所以需要特殊处理
9698

99+
```python
100+
class Solution:
101+
def evalRPN(self, tokens: List[str]) -> int:
102+
nums = []
103+
for t in tokens:
104+
if len(t) > 1 or t.isdigit():
105+
nums.append(int(t))
106+
else:
107+
if t == "+":
108+
nums[-2] += nums[-1]
109+
elif t == "-":
110+
nums[-2] -= nums[-1]
111+
elif t == "*":
112+
nums[-2] *= nums[-1]
113+
else:
114+
nums[-2] = int(nums[-2] / nums[-1])
115+
nums.pop()
116+
return nums[0]
97117
```
98118

99119
### **Java**
100120

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

103123
```java
124+
class Solution {
125+
public int evalRPN(String[] tokens) {
126+
Deque<Integer> stack = new ArrayDeque<>();
127+
for (String t : tokens) {
128+
if (t.length() > 1 || Character.isDigit(t.charAt(0))) {
129+
stack.push(Integer.parseInt(t));
130+
} else {
131+
int y = stack.pop();
132+
int x = stack.pop();
133+
switch (t) {
134+
case "+":
135+
stack.push(x + y);
136+
break;
137+
case "-":
138+
stack.push(x - y);
139+
break;
140+
case "*":
141+
stack.push(x * y);
142+
break;
143+
default:
144+
stack.push(x / y);
145+
break;
146+
}
147+
}
148+
}
149+
return stack.pop();
150+
}
151+
}
152+
```
153+
154+
### **Go**
155+
156+
```go
157+
func evalRPN(tokens []string) int {
158+
// https://github.com/emirpasic/gods#arraystack
159+
stack := arraystack.New()
160+
for _, token := range tokens {
161+
if len(token) > 1 || token[0] >= '0' && token[0] <= '9' {
162+
num, _ := strconv.Atoi(token)
163+
stack.Push(num)
164+
} else {
165+
y := popInt(stack)
166+
x := popInt(stack)
167+
switch token {
168+
case "+":
169+
stack.Push(x + y)
170+
case "-":
171+
stack.Push(x - y)
172+
case "*":
173+
stack.Push(x * y)
174+
default:
175+
stack.Push(x / y)
176+
}
177+
}
178+
}
179+
return popInt(stack)
180+
}
181+
182+
func popInt(stack *arraystack.Stack) int {
183+
v, _ := stack.Pop()
184+
return v.(int)
185+
}
186+
```
104187

188+
### **C++**
189+
190+
```cpp
191+
class Solution {
192+
public:
193+
int evalRPN(vector<string>& tokens) {
194+
stack<int> stk;
195+
for (const auto& token : tokens) {
196+
if (token.size() > 1 || isdigit(token[0])) {
197+
stk.push(stoi(token));
198+
} else {
199+
int y = stk.top();
200+
stk.pop();
201+
int x = stk.top();
202+
stk.pop();
203+
if (token[0] == '+') stk.push(x + y);
204+
else if (token[0] == '-') stk.push(x - y);
205+
else if (token[0] == '*') stk.push(x * y);
206+
else stk.push(x / y);
207+
}
208+
}
209+
return stk.top();
210+
}
211+
};
105212
```
106213
107214
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int evalRPN(vector<string>& tokens) {
4+
stack<int> stk;
5+
for (const auto& token : tokens) {
6+
if (token.size() > 1 || isdigit(token[0])) {
7+
stk.push(stoi(token));
8+
} else {
9+
int y = stk.top();
10+
stk.pop();
11+
int x = stk.top();
12+
stk.pop();
13+
if (token[0] == '+') stk.push(x + y);
14+
else if (token[0] == '-') stk.push(x - y);
15+
else if (token[0] == '*') stk.push(x * y);
16+
else stk.push(x / y);
17+
}
18+
}
19+
return stk.top();
20+
}
21+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
func evalRPN(tokens []string) int {
2+
// https://github.com/emirpasic/gods#arraystack
3+
stack := arraystack.New()
4+
for _, token := range tokens {
5+
if len(token) > 1 || token[0] >= '0' && token[0] <= '9' {
6+
num, _ := strconv.Atoi(token)
7+
stack.Push(num)
8+
} else {
9+
y := popInt(stack)
10+
x := popInt(stack)
11+
switch token {
12+
case "+":
13+
stack.Push(x + y)
14+
case "-":
15+
stack.Push(x - y)
16+
case "*":
17+
stack.Push(x * y)
18+
default:
19+
stack.Push(x / y)
20+
}
21+
}
22+
}
23+
return popInt(stack)
24+
}
25+
26+
func popInt(stack *arraystack.Stack) int {
27+
v, _ := stack.Pop()
28+
return v.(int)
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int evalRPN(String[] tokens) {
3+
Deque<Integer> stack = new ArrayDeque<>();
4+
for (String t : tokens) {
5+
if (t.length() > 1 || Character.isDigit(t.charAt(0))) {
6+
stack.push(Integer.parseInt(t));
7+
} else {
8+
int y = stack.pop();
9+
int x = stack.pop();
10+
switch (t) {
11+
case "+":
12+
stack.push(x + y);
13+
break;
14+
case "-":
15+
stack.push(x - y);
16+
break;
17+
case "*":
18+
stack.push(x * y);
19+
break;
20+
default:
21+
stack.push(x / y);
22+
break;
23+
}
24+
}
25+
}
26+
return stack.pop();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def evalRPN(self, tokens: List[str]) -> int:
3+
nums = []
4+
for t in tokens:
5+
if len(t) > 1 or t.isdigit():
6+
nums.append(int(t))
7+
else:
8+
if t == "+":
9+
nums[-2] += nums[-1]
10+
elif t == "-":
11+
nums[-2] -= nums[-1]
12+
elif t == "*":
13+
nums[-2] *= nums[-1]
14+
else:
15+
nums[-2] = int(nums[-2] / nums[-1])
16+
nums.pop()
17+
return nums[0]

0 commit comments

Comments
 (0)