GuilinDev

Lc0150

05 August 2008

150 - 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.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.

Example 1:

1
2
3
Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

1
2
3
Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

1
2
3
4
5
6
7
8
9
10
Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation: 
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

题意和分析

将操作数放前面,操作符后置的写法是逆波兰表达式。从前往后遍历数组,遇到数字则压入栈中,遇到符号,则把栈顶的两个数字拿出来运算,把结果再压入栈中,直到遍历完整个数组,栈顶数字即为最终答案。

代码

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
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if (token.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if (token.equals("-")) {
                // stack上面的是减数,下面的是被减数
                int a = stack.pop();
                int b = stack.pop();
                stack.push(b- a);
            } else if (token.equals("*")) {
                stack.push(stack.pop() * stack.pop());
            } else if (token.equals("/")) {
                // stack上面的是除数,下面的是被除数
                int a = stack.pop();
                int b = stack.pop();
                stack.push(b / a);
            } else {
                // 遇到数字直接压入栈
                stack.push(Integer.parseInt(token));
            }
        }
        return stack.pop();
    }
}

也可以用递归

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
class Solution {
    int index;
    public int evalRPN(String[] tokens) {
        if (tokens == null || tokens.length == 0) {
            return 0;
        }
        index = tokens.length - 1;
        return recursion(tokens);
    }
    private int recursion(String[] tokens) {
        String token = tokens[index];
        index--;
        
        if (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")) {
            
            // 遇到符号,先递归找到离该符号左边最近的两个数
            int b = recursion(tokens);
            int a = recursion(tokens);
            
            // 计算后,递归栈记录返回的数字
            if (token.equals("+")) {
                return a + b;
            } else if (token.equals("-")) {
                return a - b;
            } else if (token.equals("*")) {
                return a * b;
            } else {
                return a / b;
            }
            
        } else { // 遇到数字,直接返回上一层递归,准备给符号用
            return Integer.parseInt(token); // 递归栈记录返回的数字
        }
    }
}

“茴”字的第三种写法,每次遇到操作符就运算之前的两个数字(一定有的)

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
class Solution {
    public int evalRPN(String[] tokens) {
        int[] ls = new int[tokens.length/2+1];
        int index = 0;
        for (String token : tokens) {
            switch (token) {
                case "+":
                    ls[index - 2] = ls[index - 2] + ls[index - 1];
                    index--;
                    break;
                case "-":
                    ls[index - 2] = ls[index - 2] - ls[index - 1];
                    index--;
                    break;
                case "*":
                    ls[index - 2] = ls[index - 2] * ls[index - 1];
                    index--;
                    break;
                case "/":
                    ls[index - 2] = ls[index - 2] / ls[index - 1];
                    index--;
                    break;
                default:
                    ls[index++] = Integer.parseInt(token);
                    break;
            }
        }
        return ls[0];
    }
}