LeetCode[150]逆波兰表达式求值
思路:
这道题我做的太蠢了,我还多余用了一共方法来判断是不是数字,我的天啊,最后才发现只要判断是不是加减乘除就完了,判断是不是数字干嘛?
class Solution {public int evalRPN(String[] tokens) {Deque<Integer> stack = new LinkedList();for (String s : tokens) {if ("+".equals(s)) { // leetcode 内置jdk的问题,不能使用==判断字符串是否相等stack.push(stack.pop() + stack.pop()); // 注意 - 和/ 需要特殊处理} else if ("-".equals(s)) {stack.push(-stack.pop() + stack.pop());} else if ("*".equals(s)) {stack.push(stack.pop() * stack.pop());} else if ("/".equals(s)) {int temp1 = stack.pop();int temp2 = stack.pop();stack.push(temp2 / temp1);} else {stack.push(Integer.valueOf(s));}}return stack.pop();}
}