Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
Input: 123
Output: 321
Example 2:
Input: -123
Output: -321
Example 3:
Input: 120
Output: 21
Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
This is a JavaScript specific solution. It is esay to write but slow to run because it generates O(n) space. This could end up a huge array.
/**
* @param {number} x
* @return {number}
*/
let reverse = function(x) {
let n = Math.abs(x).toString().split('').reverse().join('')
if (n > 2147483647) { return 0 }
return (x < 0? -1: 1) * n
};
Pure mathamatical solution.
/**
* @param {number} x
* @return {number}
*/
let reverse = function(x) {
let result = 0
while (x) {
result = result * 10 + x % 10
x = x / 10 | 0
}
return Math.abs(result) > 2147483647 ? 0 : result
};