-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007-reverseInteger.js
More file actions
52 lines (48 loc) · 991 Bytes
/
007-reverseInteger.js
File metadata and controls
52 lines (48 loc) · 991 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* 【Reverse Integer】
* Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
注: int类型溢出return 0
int范围:[-(2^32-1) , 2^32-1 ]
*/
var reverse = function(x) {
var f = 1;
if(x === 0){
return 0;
} else if(x < 0){
f = -1;
x = -x;
}
var arr = (x + '').split('');
var arr2 = [];
for(var i = arr.length-1; i>=0; i--){
arr2.push(arr[i]);
}
var s = f*parseInt(arr2.join(''));
if(s > 2147483647 || s < -2147483647) {
return 0;
} else {
return s;
}
};
// console.log(reverse(123));
/*
Problem:https://leetcode.com/problems/reverse-integer/#/description
Submission Details:https://leetcode.com/submissions/detail/102588199/
Solution:
*/
// 其他方法1
var reverse2 = function(x) {
var res = 0;
while(x !== 0){
res = res*10 + x%10;
x = parseInt(x/10);
}
if(res > 2147483647 || res < -2147483647) {
return 0;
} else {
return res;
}
};
console.log(reverse3(-123888888));