讨论/《中级算法》 - 逆波兰表达式求值/
《中级算法》 - 逆波兰表达式求值
共 1 个回复
Deque<Integer> stack = new LinkedList<>();
        int a,b;
        for(String token : tokens){
            switch (token){
                case "*":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b * a);
                    break;
                case "/":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b / a);
                    break;
                case "+":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b + a);
                    break;
                case "-":
                    a = stack.pop();
                    b = stack.pop();
                    stack.push(b - a);
                    break;
                default:
                    stack.push(Integer.valueOf(token));
            }
        }
        return stack.peekLast();