Javascript O(n) I think it works well


  • 0
    S

    /**

    • @param {number} x

    • @return {number}
      */
      var reverse = function(x) {
      let res;

      if( x > 2147483647 || x < -2147483648 ){
      return 0;
      }

      if(x>0){
      res = x.toString().split("").reverse().join("");
      }else if(x<0){
      x = -x;
      res = x.toString().split("").reverse().join("");
      res = -(parseInt(res));
      }else{
      res = 0;
      }

      res = parseInt(res);

      if( res > 2147483647 || res < -2147483648 ){
      return 0;
      }

      return res;
      };


Log in to reply
 

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