Skip to content

Latest commit

 

History

History
29 lines (27 loc) · 908 Bytes

150. Evaluate Reverse Polish Notation.md

File metadata and controls

29 lines (27 loc) · 908 Bytes

150. Evaluate Reverse Polish Notation

LeetCode - The World's Leading Online Programming Learning Platform

class Solution {
    public int evalRPN(String[] tokens) {
        int temp;
        Stack<Integer> stack = new Stack<>();
        for (String s : tokens)
        {
            if (s.equals("+")) {
                stack.add(stack.pop() + stack.pop());
            }else if (s.equals("-")) {
                temp = stack.pop();
                stack.add(stack.pop() - temp);
            }else if (s.equals("*")){
                stack.add(stack.pop() * stack.pop());
            }else if (s.equals("/")){
                temp = stack.pop();
                stack.add(stack.pop() / temp);
            }else {
                stack.add(Integer.parseInt(s));
            }
        }
        return stack.pop();
    }
}