Created
November 30, 2018 16:36
-
-
Save auser/3a4371023bd8c75c61112acd2223480c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} x | |
* @return {number} | |
*/ | |
var reverse = function(x) { | |
let y = 0; | |
let num = Math.abs(x); | |
const intMax = Math.pow(2, 31) + 1; | |
while(num != 0) { | |
let temp = num % 10; // last digit | |
num = Math.floor(num/10); // update x (remove last digit) | |
y = (y * 10) + temp; | |
} | |
if (y > intMax) return 0; | |
return Math.sign(x) * y; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment