Skip to content

Commit 0a3b402

Browse files
committed
Add solution 150
1 parent a379699 commit 0a3b402

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Complete solutions to Leetcode problems, updated daily.
5858
| 092 | [Reverse Linked List II](https://github.com/doocs/leetcode/tree/master/solution/092.Reverse%20Linked%20List%20II) | `Linked List` |
5959
| 094 | [Binary Tree Inorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/094.Binary%20Tree%20Inorder%20Traversal) | `Hash Table`, `Stack`, `Tree` |
6060
| 144 | [Binary Tree Preorder Traversal](https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal) | `Stack`, `Tree` |
61+
| 150 | [Evaluate Reverse Polish Notation](https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation) | `Stack` |
6162
| 153 | [Find Minimum in Rotated Sorted Array](https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array) | `Array`, `Binary Search` |
6263

6364

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
## 逆波兰表达式求值
2+
### 题目描述
3+
4+
根据逆波兰表示法,求表达式的值。
5+
6+
有效的运算符包括 `+`, `-`, `*`, `/` 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
7+
8+
说明:
9+
10+
整数除法只保留整数部分。
11+
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
12+
示例 1:
13+
```
14+
输入: ["2", "1", "+", "3", "*"]
15+
输出: 9
16+
解释: ((2 + 1) * 3) = 9
17+
```
18+
19+
示例 2:
20+
```
21+
输入: ["4", "13", "5", "/", "+"]
22+
输出: 6
23+
解释: (4 + (13 / 5)) = 6
24+
```
25+
26+
示例 3:
27+
```
28+
输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
29+
输出: 22
30+
解释:
31+
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
32+
= ((10 * (6 / (12 * -11))) + 17) + 5
33+
= ((10 * (6 / -132)) + 17) + 5
34+
= ((10 * 0) + 17) + 5
35+
= (0 + 17) + 5
36+
= 17 + 5
37+
= 22
38+
```
39+
40+
### 解法
41+
遍历数组,若遇到操作数,将其压入栈中;若遇到操作符,从栈中弹出右操作数和左操作数,将运算结果压入栈中。最后栈中唯一的元素就是结果。
42+
43+
44+
```java
45+
class Solution {
46+
public int evalRPN(String[] tokens) {
47+
Stack<Integer> stack = new Stack<>();
48+
for (String e : tokens) {
49+
if (isNum(e)) {
50+
stack.push(Integer.parseInt(e));
51+
} else {
52+
int y = stack.pop();
53+
int x = stack.pop();
54+
int z = 0;
55+
switch (e) {
56+
case "+": z = x + y; break;
57+
case "-": z = x - y; break;
58+
case "*": z = x * y; break;
59+
case "/": z = x / y; break;
60+
}
61+
stack.push(z);
62+
}
63+
}
64+
return stack.peek();
65+
}
66+
67+
private boolean isNum(String val) {
68+
try {
69+
Integer.parseInt(val);
70+
return true;
71+
} catch (Exception e) {
72+
return false;
73+
}
74+
}
75+
}
76+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int evalRPN(String[] tokens) {
3+
Stack<Integer> stack = new Stack<>();
4+
for (String e : tokens) {
5+
if (isNum(e)) {
6+
stack.push(Integer.parseInt(e));
7+
} else {
8+
int y = stack.pop();
9+
int x = stack.pop();
10+
int z = 0;
11+
switch (e) {
12+
case "+": z = x + y; break;
13+
case "-": z = x - y; break;
14+
case "*": z = x * y; break;
15+
case "/": z = x / y; break;
16+
}
17+
stack.push(z);
18+
}
19+
}
20+
return stack.peek();
21+
}
22+
23+
private boolean isNum(String val) {
24+
try {
25+
Integer.parseInt(val);
26+
return true;
27+
} catch (Exception e) {
28+
return false;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)