代码不变,电脑编译器输出结果正常,但为什么leetcode上结果不对啊。剑指 Offer II 036. 后缀表达式

有大佬知道为什么吗?简单的后缀表达式,但是leetcode输出结果总为0,本机Idea上运行结果正常。

```java
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<Integer>();
        for(int i=0;i<tokens.length;i++)
            try{
                int num=Integer.valueOf(tokens[i]);
                stack.push(num);
            }catch(Exception e){
                int b=stack.pop();
                int a=stack.pop();
                int new_num=0;
                if(tokens[i]=="+"){
                    new_num=a+b;
                }else if(tokens[i]=="-"){
                    new_num=a-b;
                }else if(tokens[i]=="*"){
                    new_num=a*b;
                }else if(tokens[i]=="/"){
                    new_num=a/b;
                }
                stack.push(new_num);
            }

        return stack.pop();
    
    }
}

```

这个要看你录入的数据,测试才知道问题。

把判断换一下,可能没满足判断条件。

public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < tokens.length; i++)
            try {
                int num = Integer.valueOf(tokens[i]);
                stack.push(num);
            } catch (Exception e) {
                int b = stack.pop();
                int a = stack.pop();
                int new_num = 0;
                if (tokens[i].equals( "+")) {
                    new_num = a + b;
                } else if (tokens[i].equals( "-")) {
                    new_num = a - b;
                } else if (tokens[i].equals("*")) {
                    new_num = a * b;
                } else if (tokens[i].equals( "/")) {
                    new_num = a / b;
                }
                stack.push(new_num);
            }
        return stack.pop();
    }