---
# System prepended metadata

title: 0150. Evaluate Reverse Polish Notation
tags: [Leetcode, Medium, Stack]

---

# 0150. Evaluate Reverse Polish Notation
###### tags: `Leetcode` `Medium` `Stack`
Link: https://leetcode.com/problems/evaluate-reverse-polish-notation/
## 思路
正常用stack做即可
要注意的是
首先在判断一个token是不是数字那里 要看最后一位是不是数字 因为第一位可能是负号
其次由于pop的顺序和塞进去的顺序是反的 所以减法和除法因为是第二个在前面 也就是```num2/num1```
## Code
```java=
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String token:tokens){
            if(Character.isDigit(token.charAt(token.length()-1))){
                int num = Integer.parseInt(token);
                stack.add(num);
            }
            else{
                int num1 = stack.pop();
                int num2 = stack.pop();
                int res = 0;
                if(token.equals("+")) res = num1+num2;
                else if(token.equals("-")) res = num2-num1;
                else if(token.equals("*")) res = num1*num2;
                else res = num2/num1;
                stack.add(res);
            }
        }
        return stack.pop();
    }
}
```