Reverse Integer


  • 0
    S
                int y = 0;
                int count  = 0;
                int prevY = 0;
                int r = 0;
                boolean negflag = false;
                if(x < 0){
                    x= -x;
                    negflag = true;
                }
                while(x != 0){
    
                    r = x % 10;
                    x =  x/10;
                    prevY = y;
                    y = y *10 + r;
                    if( prevY != (y - r)/10){
                        return 0;
                    }
                    count++;
                }
    
                return negflag ? -y : y;
    

Log in to reply
 

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