forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.js
36 lines (34 loc) · 1.01 KB
/
Solution.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
/**
* @param {string} s
* @return {string}
*/
var longestPalindrome = 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
}
console.log(longestPalindrome("cbbd"))