Evaluate Reverse Polish Notation

来源:互联网 发布:不属于大数据4v特征 编辑:程序博客网 时间:2024/06/05 19:21
public class Solution {    public int evalRPN(String[] tokens) {        if (tokens == null || tokens.length == 0) {            return Integer.MAX_VALUE;        }        int result = 0;        Stack<String> stack = new Stack<String>();        for (String s : tokens) {            if (s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")) {                int second = Integer.parseInt(stack.pop());                int first = Integer.parseInt(stack.pop());                if (s.equals("+")) {                    stack.push(String.valueOf(first + second));                } else if (s.equals("-")) {                    stack.push(String.valueOf(first - second));                } else if (s.equals("*")) {                    stack.push(String.valueOf(first * second));                } else if (s.equals("/")) {                    stack.push(String.valueOf(first / second));                }            } else {                stack.push(s);            }        }        return Integer.parseInt(stack.pop());       }}

0 0