求助丨这个题目我写的代码为什么不能通过力扣呢
802
2023.12.19
发布于 未知归属地

题目请教

大佬们这个题目为什么报错了,在vscode上运行并没有报错 题目是150.逆波兰表达式. 报错内容是下面的图片


public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
                if (tokens[i] == "+") {
                    int temp = stack.pop() + stack.pop();
                    stack.push(temp);
                }
                if (tokens[i] == "-") {
                    int temp = -stack.pop() + stack.pop();
                    stack.push(temp);
                }
                if (tokens[i] == "*") {
                    int temp = stack.pop() * stack.pop();
                    stack.push(temp);
                }
                if (tokens[i] == "/") {
                    int up = stack.pop();
                    int down = stack.pop();
                    stack.push(down / up);
                }
            } else {
                stack.push(Integer.valueOf(tokens[i]));
            }

        }
        return stack.pop();
    }

截屏2023-12-19 下午7.36.28.png

评论 (6)