Code is runnable on eclipse but can not pass the test case.


  • 0
    S

    The following code is runnable on eclipse but can not pass the test case: {"0", "3", "/"}.
    Any one knows why?

    public static int evalRPN(String[] tokens) 
    {
        if(tokens.length == 0)
            return 0;
        if(tokens.length == 1)
            return Integer.parseInt(tokens[0]);
        
        Stack<Integer> numbers = new Stack<Integer>();
        int result = 0;
        int tmp;
        for(int n = 0; n < tokens.length; n++)
        {
            if(!isOperator(tokens[n]))
            {
                numbers.push(Integer.parseInt(tokens[n]));
            }
            else
            {
                if(numbers.size()<2)
                    return Integer.MIN_VALUE;
                if(tokens[n] == "+")
                {
                    tmp = numbers.pop();
                    result = numbers.pop() + tmp;
                }
                else if(tokens[n] == "+")
                {
                    tmp = numbers.pop();
                    result = numbers.pop() - tmp;
                }
                else if(tokens[n] == "*")
                {
                    tmp = numbers.pop();
                    result = numbers.pop() * tmp;
                }
                else if(tokens[n] == "/")
                {
                    tmp = numbers.pop();
                    result = numbers.pop() / tmp;
                }
                else
                {
                    return Integer.MIN_VALUE;
                }
                numbers.push(result);
            }
            
        }
        return numbers.pop();
    }
    
    private static boolean isOperator(String s)
    {
        return s == "+" || s == "-" || s == "*" || s == "/";
    }

  • 0
    S

    Could you please describe your algorithm and add comment in your code? Thanks.


  • 0
    M

    hi, there are some bugs in your code..

     else if(tokens[n] == "+") should be  else if(tokens[n] == "-")
    

    and you should use equals function instead of == .


  • 3
    T

    Here, there were some minor errors in your code, this runs just fine !

    public class Solution {
        public static int evalRPN(String[] tokens) 
        {
            if(tokens.length == 0)
                return 0;
            if(tokens.length == 1)
                return Integer.parseInt(tokens[0]);
        
            Stack<Integer> numbers = new Stack<Integer>();
            int result = 0;
            int tmp;
            for(int n = 0; n < tokens.length; n++)
            {
                if(!isOperator(tokens[n]))
                {
                    numbers.push(Integer.parseInt(tokens[n]));
                }
                else
                {
                    if(numbers.size()<2)//This won't help, even if the test input is wrong
                        return Integer.MIN_VALUE;
                        
                    if(tokens[n].equals("+"))
                    {
                        tmp = numbers.pop();
                        result = numbers.pop() + tmp;
                    }
                    else if(tokens[n].equals("-"))
                    {
                        tmp = numbers.pop();
                        result = numbers.pop() - tmp;
                    }
                    else if(tokens[n].equals("*"))
                    {
                        tmp = numbers.pop();
                        result = numbers.pop() * tmp;
                    }
                    else if(tokens[n].equals("/"))
                    {
                        tmp = numbers.pop();
                        result = numbers.pop() / tmp;
                    }
                    else
                    {//This won't help, even if the test input is wrong
                        return Integer.MIN_VALUE;
                    }
                    numbers.push(result);
                }
        
            }
            return numbers.pop();
        }
        
        private static boolean isOperator(String s)
        {
            s.trim();//remove any trailing whitespaces.
            /*
            Use <string>.equals to compare strings, like mblank has pointed out
            */
            return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
        }
    }

Log in to reply
 

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