-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path150.逆波兰表达式求值.java
42 lines (35 loc) · 983 Bytes
/
150.逆波兰表达式求值.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* @lc app=leetcode.cn id=150 lang=java
*
* [150] 逆波兰表达式求值
*/
// @lc code=start
import java.utils;
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack=new Stack<>();
for(String s:tokens){
if(s.equals("/")){
int a=stack.pop();
int b=stack.pop();
stack.push(b/a);
}else if(s.equals("*")){
int a=stack.pop();
int b=stack.pop();
stack.push(a*b);
}else if(s.equals("+")){
int a=stack.pop();
int b=stack.pop();
stack.push(a+b);
}else if(s.equals("-")){
int a=stack.pop();
int b=stack.pop();
stack.push(b-a);
}else{
stack.push(Integer.valueOf(s));
}
}
return stack.peek();
}
}
// @lc code=end