巧妙避开对数字字符串的判断
import java.util.*; public class Solution { /** * * @param tokens string字符串一维数组 * @return int整型 */ public int evalRPN (String[] tokens) { // write code here Stack<Integer> stk = new Stack<>(); for (int i = 0; i < tokens.length; ++i) { String s = tokens[i]; if (s.equals("+")) { int a = stk.pop(); int b = stk.pop(); stk.push(a+b); } else if (s.equals("-")) { int a = stk.pop(); int b = stk.pop(); stk.push(b-a); } else if (s.equals("*")) { int a = stk.pop(); int b = stk.pop(); stk.push(b*a); } else if (s.equals("/")) { int a = stk.pop(); int b = stk.pop(); stk.push(b/a); } else { stk.push(Integer.valueOf(s)); } } return stk.pop(); } }