import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param tokens string字符串一维数组 * @return int整型 */ public int evalRPN (String[] tokens) { // write code here if (tokens == null || tokens.length < 1) { throw new IllegalArgumentException("bad params"); } Stack<Integer> stack = new Stack<>(); for (String token : tokens) { if (isNumber(token)) { stack.push(Integer.parseInt(token)); } else { int second = stack.pop(); int first = stack.pop(); int tmp = 0; if (token.equals("+")) { tmp = first + second; } else if (token.equals("-")) { tmp = first - second; } else if (token.equals("*")) { tmp = first * second; } else if (token.equals("/")) { tmp = first / second; } else { throw new IllegalArgumentException("Illegal operator"); } stack.push(tmp); } } return stack.pop(); } private boolean isNumber(String str) { char[] arr = str.toCharArray(); for (int i = 0; i < arr.length; i++) { if (i == 0 && arr.length > 1 && arr[i] == '-') { continue; } if (arr[i] < '0' || arr[i] > '9') { return false; } } return true; } }