Why my code is RunTime Error?


  • 0
    C
    enter code here
    

    public static int evalRPN(String[] tokens)
    {

       Stack<Integer> stack=new Stack<Integer>(); 
       int k=0,j=0,i=0;
       for(i=0;i<tokens.length;i++)
       {
    	   String str=tokens[i];
           if(str=="+"||str=="-"||str=="*"||str=="/")
           {
        	        k=stack.pop();
        		    j=stack.pop();
        		    if(str=="+")
        		    	stack.push(k+j);
        		    if(str=="-")
        		    	stack.push(k-j);
        		    if(str=="*")
        		    	stack.push(k*j);
        		    if(str=="/")
        		    {
        		    	if(j==0)
        		    		return 0;
        		    	else
        		    		stack.push(k/j);
        		    }
           }
           else
        	   stack.push(Integer.parseInt(str));
       }
       return stack.pop();
    }

  • 0
    J

    k and j should be reversed.

    Please provide more information - at least 30 characters


Log in to reply
 

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