Skip to content

Commit 748d1b6

Browse files
committed
feat: update solutions to lc problem: No.0150.Evaluate Polish Notation
1 parent dfee25a commit 748d1b6

File tree

10 files changed

+382
-177
lines changed

10 files changed

+382
-177
lines changed

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

+49-47
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,19 @@
8181

8282
<p><meta charset="UTF-8" />注意:本题与主站 150&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/">https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/</a></p>
8383

84-
8584
## 解法
8685

8786
<!-- 这里可写通用的实现逻辑 -->
8887

89-
利用栈存储运算数,每次遇到符号,对栈顶两个元素进行运算
88+
利用栈存储运算数,每次遇到符号,对栈顶两个元素进行运算
9089

9190
<!-- tabs:start -->
9291

9392
### **Python3**
9493

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

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

9998
```python
10099
class Solution:
@@ -123,60 +122,89 @@ class Solution:
123122
```java
124123
class Solution {
125124
public int evalRPN(String[] tokens) {
126-
Deque<Integer> stack = new ArrayDeque<>();
125+
Deque<Integer> stk = new ArrayDeque<>();
127126
for (String t : tokens) {
128127
if (t.length() > 1 || Character.isDigit(t.charAt(0))) {
129-
stack.push(Integer.parseInt(t));
128+
stk.push(Integer.parseInt(t));
130129
} else {
131-
int y = stack.pop();
132-
int x = stack.pop();
130+
int y = stk.pop();
131+
int x = stk.pop();
133132
switch (t) {
134133
case "+":
135-
stack.push(x + y);
134+
stk.push(x + y);
136135
break;
137136
case "-":
138-
stack.push(x - y);
137+
stk.push(x - y);
139138
break;
140139
case "*":
141-
stack.push(x * y);
140+
stk.push(x * y);
142141
break;
143142
default:
144-
stack.push(x / y);
143+
stk.push(x / y);
145144
break;
146145
}
147146
}
148147
}
149-
return stack.pop();
148+
return stk.pop();
150149
}
151150
}
152151
```
153152

153+
### **C++**
154+
155+
```cpp
156+
class Solution {
157+
public:
158+
int evalRPN(vector<string>& tokens) {
159+
stack<int> stk;
160+
for (auto& t : tokens) {
161+
if (t.size() > 1 || isdigit(t[0]))
162+
{
163+
stk.push(stoi(t));
164+
}
165+
else
166+
{
167+
int y = stk.top();
168+
stk.pop();
169+
int x = stk.top();
170+
stk.pop();
171+
if (t[0] == '+') stk.push(x + y);
172+
else if (t[0] == '-') stk.push(x - y);
173+
else if (t[0] == '*') stk.push(x * y);
174+
else stk.push(x / y);
175+
}
176+
}
177+
return stk.top();
178+
}
179+
};
180+
```
181+
154182
### **Go**
155183
156184
```go
157185
func evalRPN(tokens []string) int {
158186
// https://github.com/emirpasic/gods#arraystack
159-
stack := arraystack.New()
187+
stk := arraystack.New()
160188
for _, token := range tokens {
161189
if len(token) > 1 || token[0] >= '0' && token[0] <= '9' {
162190
num, _ := strconv.Atoi(token)
163-
stack.Push(num)
191+
stk.Push(num)
164192
} else {
165-
y := popInt(stack)
166-
x := popInt(stack)
193+
y := popInt(stk)
194+
x := popInt(stk)
167195
switch token {
168196
case "+":
169-
stack.Push(x + y)
197+
stk.Push(x + y)
170198
case "-":
171-
stack.Push(x - y)
199+
stk.Push(x - y)
172200
case "*":
173-
stack.Push(x * y)
201+
stk.Push(x * y)
174202
default:
175-
stack.Push(x / y)
203+
stk.Push(x / y)
176204
}
177205
}
178206
}
179-
return popInt(stack)
207+
return popInt(stk)
180208
}
181209
182210
func popInt(stack *arraystack.Stack) int {
@@ -185,32 +213,6 @@ func popInt(stack *arraystack.Stack) int {
185213
}
186214
```
187215

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-
};
212-
```
213-
214216
### **...**
215217

216218
```

lcof2/剑指 Offer II 036. 后缀表达式/Solution.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ class Solution {
22
public:
33
int evalRPN(vector<string>& tokens) {
44
stack<int> stk;
5-
for (const auto& token : tokens) {
6-
if (token.size() > 1 || isdigit(token[0])) {
7-
stk.push(stoi(token));
8-
} else {
5+
for (auto& t : tokens) {
6+
if (t.size() > 1 || isdigit(t[0]))
7+
{
8+
stk.push(stoi(t));
9+
}
10+
else
11+
{
912
int y = stk.top();
1013
stk.pop();
1114
int x = stk.top();
1215
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+
if (t[0] == '+') stk.push(x + y);
17+
else if (t[0] == '-') stk.push(x - y);
18+
else if (t[0] == '*') stk.push(x * y);
1619
else stk.push(x / y);
1720
}
1821
}
1922
return stk.top();
2023
}
21-
};
24+
};
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
func evalRPN(tokens []string) int {
22
// https://github.com/emirpasic/gods#arraystack
3-
stack := arraystack.New()
3+
stk := arraystack.New()
44
for _, token := range tokens {
55
if len(token) > 1 || token[0] >= '0' && token[0] <= '9' {
66
num, _ := strconv.Atoi(token)
7-
stack.Push(num)
7+
stk.Push(num)
88
} else {
9-
y := popInt(stack)
10-
x := popInt(stack)
9+
y := popInt(stk)
10+
x := popInt(stk)
1111
switch token {
1212
case "+":
13-
stack.Push(x + y)
13+
stk.Push(x + y)
1414
case "-":
15-
stack.Push(x - y)
15+
stk.Push(x - y)
1616
case "*":
17-
stack.Push(x * y)
17+
stk.Push(x * y)
1818
default:
19-
stack.Push(x / y)
19+
stk.Push(x / y)
2020
}
2121
}
2222
}
23-
return popInt(stack)
23+
return popInt(stk)
2424
}
2525

2626
func popInt(stack *arraystack.Stack) int {
2727
v, _ := stack.Pop()
2828
return v.(int)
29-
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
class Solution {
22
public int evalRPN(String[] tokens) {
3-
Deque<Integer> stack = new ArrayDeque<>();
3+
Deque<Integer> stk = new ArrayDeque<>();
44
for (String t : tokens) {
55
if (t.length() > 1 || Character.isDigit(t.charAt(0))) {
6-
stack.push(Integer.parseInt(t));
6+
stk.push(Integer.parseInt(t));
77
} else {
8-
int y = stack.pop();
9-
int x = stack.pop();
8+
int y = stk.pop();
9+
int x = stk.pop();
1010
switch (t) {
1111
case "+":
12-
stack.push(x + y);
12+
stk.push(x + y);
1313
break;
1414
case "-":
15-
stack.push(x - y);
15+
stk.push(x - y);
1616
break;
1717
case "*":
18-
stack.push(x * y);
18+
stk.push(x * y);
1919
break;
2020
default:
21-
stack.push(x / y);
21+
stk.push(x / y);
2222
break;
2323
}
2424
}
2525
}
26-
return stack.pop();
26+
return stk.pop();
2727
}
28-
}
28+
}

0 commit comments

Comments
 (0)