标签:
/* *150. Evaluate Reverse Polish Notation * 1.1 by Mingyang * 这里我开始想的是把string变成char再来判断是数字还是符号,但是我发现错了!因为这里string变成char非常难!!! * 所以Integer.parseInt(tokens[i]就可以很好的解决这个问题,直接变为int,包括你的符号!!!! */ public int evalRPN(String[] tokens) { Stack<Integer> stack = new Stack<Integer>(); int a, b; for (int i = 0; i < tokens.length; i++) { if (tokens[i].equals("+")) { a = stack.pop(); b = stack.pop(); stack.push(b + a); } else if (tokens[i].equals("-")) { a = stack.pop(); b = stack.pop(); stack.push(b - a); } else if (tokens[i].equals("*")) { a = stack.pop(); b = stack.pop(); stack.push(b * a); } else if (tokens[i].equals("/")) { a = stack.pop(); b = stack.pop(); stack.push(b / a); } else { stack.push(Integer.parseInt(tokens[i])); } } return stack.pop(); }
150. Evaluate Reverse Polish Notation
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5551954.html