Test case failing


  • 0
    M

    The below code is failing in the test case
    Input: ["0","3","/"]
    Output: -1
    Expected: 0
    However it runs successfully on my local machine. Please can anyone point out the problem.
    import java.util.Stack;

    public class Solution {

       public int evalRPN(String[] tokens) {
        int result =0;
        int result2 =0;
        Stack<Integer> stack = new Stack<Integer>();
        for(int index= 0; index< tokens.length;index++){
            String token = tokens[index];
            if(token == "+" ||token == "-" ||token == "*" ||token == "/"){
                result = stack.pop();
                result2 = stack.pop();
                if(token == "+")
                result = result + result2;
                if(token == "-")
                    result = result - result2;
                if(token == "*")
                    result = result * result2;
                if(token == "/")
                    result = result2 / result;
                stack.push(result);
            }
            else{
                int result3=0;
                for(int i =0;i<token.length();i++){
                    result3 = result3*10 + token.charAt(i) - '0';
                }
                stack.push(result3);
            }
    
        }
        return stack.pop();
    }
    

    }


  • 1
    I

    { (token == "+") ...... }

    all the comparisons should not use == , use .equals() instead.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.