Javascript solution for Reversed Integer


  • 3
    S
    if (x < 0) return -reverse(-x);
    
    var reversedInt = 0;
    
    while(x>0) {
        var a = x%10;
        x = Math.floor(x/10);
        
        //Should no larger than Math.floor(Number.MAX_VALUE/10)
        if(reversedInt >= 214748365)
            return 0;
        reversedInt = reversedInt*10+a;
    }
    
    return reversedInt;
    

    For Javascript, I guess just using Number.MAX_VALUE or Number.MAX_SAFE_VALUE works instead of using specific number.


Log in to reply
 

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