Leetcode-evaluate-reverse-polish-notation

题目描述

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are+,-,*,/. Each operand may be an integer or another expression.

Some examples:

["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

这一题是考波兰数,我们可以通过栈来解决,将数字放入栈中,读到运算符就从栈中拿两个数出来运算,然后将运算结果放进栈中,到执行结束后栈中剩下的数就是结果。

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
import java.util.*;

public class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<Integer>();
int len = tokens.length;
for(int i = 0; i < len; i++){
if(tokens[i].equals("+")){
int t1 = stack.pop();
int t2 = stack.pop();
stack.push(t2+t1);
}else if(tokens[i].equals("-")){
int t1 = stack.pop();
int t2 = stack.pop();
stack.push(t2-t1);
}else if(tokens[i].equals("*")){
int t1 = stack.pop();
int t2 = stack.pop();
stack.push(t2*t1);
}else if(tokens[i].equals("/")){
int t1 = stack.pop();
int t2 = stack.pop();
stack.push(t2/t1);
}else{
stack.push(Integer.parseInt(tokens[i]));
}
}
return stack.pop();
}
}