Skip to content

Commit db08b24

Browse files
authored
Update 7-reverse-integer.js
1 parent a3382d9 commit db08b24

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

7-reverse-integer.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/**
2+
* @param {number} x
3+
* @return {number}
4+
*/
5+
const reverse = function (x) {
6+
let res = 0, tail, newResult
7+
const low = -Math.pow(2, 31), high = Math.pow(2, 31)
8+
while(x !== 0) {
9+
tail = x % 10
10+
newResult = res * 10 + tail
11+
// if((newResult - tail) / 10 !== res) return 0
12+
if(newResult < low || newResult >= high) return 0
13+
res = newResult
14+
x = ~~(x / 10)
15+
}
16+
17+
return res
18+
};
19+
20+
// another
21+
22+
123
/**
224
* @param {number} x
325
* @return {number}

0 commit comments

Comments
 (0)