-
Notifications
You must be signed in to change notification settings - Fork 4
/
9.Palindrome_Number.js
58 lines (57 loc) · 1.08 KB
/
9.Palindrome_Number.js
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
/**
* @param {number} x
* @return {boolean}
*/
var isPalindrome = function (x) {
// 解法4
if (x < 0) return false;
const y = Math.abs(x).toString();
let i = 0;
let j = y.length - 1;
while (i < j) {
if (y[i] !== y[j]) {
return false;
}
i++;
j--;
}
return true
/**
* 解法1
* 性能:344ms 46.1MB
*/
var strX = x + "";
var strArr = [];
var strLength = strX.length;
var isPalindrome = true;
for (var i = 0; i < strLength; i++) {
strArr.push(strX.charAt(i));
}
var strArrReverse = strArr.map((e) => e).reverse();
strArr.forEach((e, i) => {
if (e !== strArrReverse[i]) {
isPalindrome = false;
}
});
return isPalindrome;
/**
* 解法2:Array.prototype.reverse
* 性能:240ms 45.8MB
*/
return `${x}` === `${x}`.split("").reverse().join("");
/**
* 解法3:对撞指针法
* 性能:244ms 45.5MB
*/
var strArr = `${x}`.split("");
var i = 0;
var j = strArr.length - 1;
while (i < j) {
if (strArr[i] !== strArr[j]) {
return false;
}
i++;
j--;
}
return true;
};