-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.reverse-integer.js
More file actions
65 lines (55 loc) · 1.39 KB
/
7.reverse-integer.js
File metadata and controls
65 lines (55 loc) · 1.39 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
/* link: https://leetcode.com/problems/reverse-integer/
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
*/
// 思路很简单直接,提取整数的每一位,反转,再拼凑出结果就ok了。// 注意整数溢出
/*
* @param {number} x
* @return {number}
*/
var reverse = function(x) {
var arr = [];
var pivot = 10;
var temp = x >= 0 ? x : -x;
while (pivot <= temp) {
pivot *= 10;
}
pivot = pivot / 10;
var rem;
while (temp > 0) {
rem = Math.floor(temp / pivot);
arr.push(rem);
temp -= pivot * rem;
pivot /= 10;
}
var res = 0;
for (var i = arr.length - 1; i >= 0; --i) {
res += arr[i] * Math.pow(10, i);
}
res = x >= 0 ? res : -res;
// 整数溢出判断
if (res > 2147483647 || res < -2147483647) {
res = 0;
}
return res;
};
// 下面这种方法显然比我高明得多,来自这里:https://leetcode.com/discuss/84344/share-my-answer
var reverse1 = function(x) {
var res = 0;
var temp = x;
if (temp < 0) {
temp = -temp;
}
while (temp) {
res = res * 10 + temp % 10;
temp = Math.floor(temp / 10);
}
res = x < 0 ? -res : res;
// 整数溢出判断
if (res > 2147483647 || res < -2147483647) {
res = 0;
}
return res;
};
console.log(reverse1(-1233434));