Why my code doesn't work here? It works in Eclipse.


  • 0
    S
        public int evalRPN(String[] tokens) {
        
        int count = tokens.length;
        int result = 0;
        
        for (int i = 0; i < count - 2; i++) {
            if (!isOperator(tokens[i]) && !isOperator(tokens[i+1]) && isOperator(tokens[i+2])) {
                
                int a = Integer.parseInt(tokens[i]);
                int b = Integer.parseInt(tokens[i+1]);
                
                if (tokens[i+2] == "+")
                        result = a + b;
                if (tokens[i+2] == "-")
                        result = a - b;
                if (tokens[i+2] == "*")
                        result = a * b;
                if (tokens[i+2] == "/")
                        result = a / b;
                        
                tokens[i] = String.valueOf(result);
                
                for (int j = i + 1; j < count - 2; j++)
                    tokens[j] = tokens[j+2];
                
                count = count - 2;
                i = -1;
            }
        }
        
        return Integer.parseInt(tokens[0]);
    }
    
    private boolean isOperator(String s) {
        if (s == "+" || s == "-" || s == "*" || s == "/")
            return true;
        else
            return false;
    }

Log in to reply
 

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