-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainpage.html
42 lines (40 loc) · 963 Bytes
/
mainpage.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
test
</body>
<script>
var longestPlaindrome = function (s) {
let maxLength = 0, left = 0, right = 0;
for (let i = 0; i < s.length; i++) {
let singleCharLength = getPallenByCenterChar(s, i, i);
let doubleCharLength = getPallenByCenterChar(s, i, i + 1)
let max = Math.max(singleCharLength, doubleCharLength);
if (max > maxLength) {
maxLength = max;
left = i - parseInt((max - 1) / 2);
right = i + parseInt(max - 2)
}
}
return s.slice(left, right + 1)
};
function getPallenByCenterChar(s, left, right) {
// 中间值为两个字符串,确保两个字符相等
if (s[left] != s[right]) {
return right - left; // 不相等返回为1个字符串
}
while (left > 0 && right < s.length - 1) {
// 先加减再判断
left--;
right++;
if (s[left] != s[right]) {
return right - left - 1;
}
}
return right - left + 1;
}
</script>
</html>