leetcode Evaluate Reverse Polish Notation

题目链接

思路:
这个题还好是让你计算波兰式。如果说让你把波兰是给出来,那就亮瞎狗眼了。。。。。
道理很简单。看看书都懂了,不懂的百度一下。。

public class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<>();
        int i=0;
        while(i<tokens.length)
        {
            if(Character.isDigit(tokens[i].charAt(0))||tokens[i].length()>1)
            {
                stack.push(Integer.valueOf(tokens[i]));
            }
            else
            {
                int number2=stack.pop();
                int number1=stack.pop();

                switch (tokens[i]) {
                case "+":
                    stack.push(number1+number2);
                    break;
                case "-":
                    stack.push(number1-number2);
                    break;
                case "*":
                    stack.push(number1*number2);
                    break;
                case "/":
                    stack.push(number1/number2);
                    break;

                default:
                    break;
                }
            }
            i++;
        }
        return stack.pop();
    }
}

你可能感兴趣的:(leetcode Evaluate Reverse Polish Notation)