forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0007-reverse-integer.js
More file actions
30 lines (24 loc) · 836 Bytes
/
0007-reverse-integer.js
File metadata and controls
30 lines (24 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* https://leetcode.com/problems/reverse-integer/
* Time O(log(x)) | Space O(1)
* @param {number} x
* @return {number}
*/
var reverse = function (x, result = 0) {
while (x !== 0) {
const digit = x % 10;
if (isOutOfBounds(digit, result)) return 0;
x = Math.trunc(x / 10);
result = result * 10 + digit;
}
return result;
};
const isOutOfBounds = (digit, result) => {
const [max, min] = [2 ** 31 - 1, -(2 ** 31)];
const [maxProduct, maxRemainder] = [max / 10, max % 10];
const [minProduct, minRemainder] = [min / 10, min % 10];
const isTarget = result === maxProduct;
const isMaxOut = maxProduct < result || (isTarget && maxRemainder <= digit);
const isMinOut = result < minProduct || (isTarget && digit <= minRemainder);
return isMaxOut || isMinOut;
};