---
# System prepended metadata

title: 0150. 逆波兰表达式
tags: [Leetcode]

---

###### tags: `Leetcode`
# 0150. 逆波兰表达式
Link: https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/solution/ni-bo-lan-biao-da-shi-qiu-zhi-by-leetcod-wue9/
## 思路

## Keypoints
## Code in C++

## Code in Java
```java=
class Solution {
    public int evalRPN(String[] tokens) {
        int len = tokens.length;
        Deque<Integer> stack = new ArrayDeque<>();
        for (int i = 0; i < len; i++) {
            int res = 0;
            if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")) {
                int num1 = stack.removeLast();
                int num2 = stack.removeLast();
                if (tokens[i].equals("+")) {
                    res = (num2 + num1);
                    stack.addLast(res);
                }
                else if (tokens[i].equals("-")) {
                    res = (num2 - num1);
                    stack.addLast(res);
                }
                else if (tokens[i].equals("*")) {
                    res = (num2 * num1);
                    stack.addLast(res);
                }
                else {
                    res = (num2 / num1);
                    stack.addLast(res);
                }
            } else {
                char[] charNum = tokens[i].toCharArray();
                int num = 0, bei = 1;
                for (int j = charNum.length - 1; j >= 1; j--) {
                    num += ((charNum[j] - '0') * bei);
                    bei *= 10;
                }
                if (charNum[0] == '-') num = -num;
                else num += ((charNum[0] - '0') * bei);
                stack.addLast(num);
            }
        }
        return stack.getLast();
    }
}
```
## 思路
思路很简单，用栈就行了