JavaScript Solution does not work for long integer overflows


  • 0
    G

    var reverse = function(x) {
    let isNegative = false;
    let num = x;
    if (x < 0) {
    isNegative = true;
    num = -x;
    }

    let rev = 0,
    prev_rev_num = 0;
    while (num > 0) {
    rev = rev * 10 + num % 10;

     if (((rev - num % 10) / 10) !== prev_rev_num) {
         return 0;
     }  
       
     prev_rev_num = rev;  
     num = Math.floor(num / 10);  
    

    }

    if (isNegative) {
    rev = -rev;
    }

    return rev;
    };


Log in to reply
 

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