8 lines java code


  • 0
    C
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack=new Stack<Integer>();
        for(String token:tokens)
            if(token.equals("+")) stack.push(stack.pop()+stack.pop());      
            else if(token.equals("-")) stack.push(0-stack.pop()+stack.pop() );   
            else if(token.equals("*")) stack.push(stack.pop()*stack.pop());    
            else if(token.equals("/")) stack.push((int)(((double)1/stack.pop())*(double)stack.pop()));
            else stack.push(Integer.valueOf(token));
        return stack.pop();
    }

Log in to reply
 

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