有大佬知道为什么吗?简单的后缀表达式,但是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();
}