Skip to content

Commit 3a67ad4

Browse files
add js solution for 007
1 parent caa0927 commit 3a67ad4

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const reverse1 = function(x){
2+
let s = String(x);
3+
let isNegative = false;
4+
if(s[0] === '-'){
5+
isNegative = true;
6+
}
7+
s = parseInt(s.split('').reverse().join(''));
8+
return isNegative ? (s > Math.pow(2,31) ? 0 : -s) : (s > Math.pow(2,31) - 1 ? 0 : s);
9+
}
10+
11+
const reverse = function(x){
12+
let result = parseInt(x.toString().split('').reverse().join(''));
13+
if(result > Math.pow(2,31) - 1 || -result < Math.pow(-2,31)) return 0;
14+
return x > 0 ? result: -result;
15+
}

0 commit comments

Comments
 (0)