Use a stack to keep track of the numbers, every time an operator is seen, pop two numbers and perform the operation.
Solution:
Time: O(n) Space: O(n)
public class Solution { public int evalRPN(ArrayList<String> A) { Deque<String> stack = new ArrayDeque<>(); int result = 0; for (String str : A) { if (isOperator(str)) { int b = Integer.valueOf(stack.pop()); int a = Integer.valueOf(stack.pop()); int temp = 0; if (str.equals("+")) { temp = a + b; } else if (str.equals("-")) { temp = a - b; } else if (str.equals("*")) { temp = a * b; } else if (str.equals("/")) { temp = a / b; } stack.push("" + temp); } else { stack.push(str); } } return Integer.valueOf(stack.pop()); }