Skip to content

Commit 0146595

Browse files
authored
Update 0150.逆波兰表达式求值 Java Version
Add Java Solution with SwitchCase Approach 添加 Java 代码 Switch Case。更简洁一些。思路一样 都是 Stack
1 parent b489cb6 commit 0146595

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

problems/0150.逆波兰表达式求值.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,40 @@ class Solution {
162162
}
163163
```
164164

165+
```Java
166+
// Switch Case 写法,更简洁一些
167+
class Solution {
168+
public int evalRPN(String[] tokens) {
169+
Stack<Integer> stk = new Stack<>();
170+
for (String token : tokens) {
171+
172+
// 运算符Operator + - * /
173+
if ("+-*/".contains(token)) {
174+
int a = stk.pop();
175+
int b = stk.pop();
176+
switch (token) {
177+
case "+":
178+
stk.push(a + b);
179+
break;
180+
case "-":
181+
stk.push(b - a);
182+
break;
183+
case "*":
184+
stk.push(a * b);
185+
break;
186+
case "/":
187+
stk.push(b / a);
188+
break;
189+
}
190+
} else {
191+
stk.push(Integer.parseInt(token));
192+
}
193+
}
194+
return stk.pop();
195+
}
196+
}
197+
```
198+
165199
### Python3:
166200

167201
```python

0 commit comments

Comments
 (0)