Skip to content

Commit 1b9cc9d

Browse files
committed
Merge branch 'main' of github.com:w2xi/leetcode
2 parents ad67476 + acfd4b5 commit 1b9cc9d

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
|396|[旋转函数](https://leetcode.cn/problems/rotate-function/)|[JavaScript](./algorithms/rotate-function.js)|Medium|
135135
|404|[左叶子之和](https://leetcode.cn/problems/sum-of-left-leaves/)|[JavaScript](./algorithms/sum-of-left-leaves.js)|Easy|
136136
|406|[根据身高重建队列](https://leetcode.cn/problems/queue-reconstruction-by-height/)|[JavaScript](./algorithms/queue-reconstruction-by-height.js)|Medium|
137+
|409|[最长回文串](https://leetcode.cn/problems/longest-palindrome/)|[JavaScript](./algorithms/longest-palindrome.js)|Easy|
137138
|412|[Fizz Buzz](https://leetcode.cn/problems/fizz-buzz/)|[JavaScript](./algorithms/fizz-buzz.js)|Easy|
138139
|414|[第三大的数](https://leetcode.cn/problems/third-maximum-number/)|[JavaScript](./algorithms/third-maximum-number.js)|Easy|
139140
|442|[数组中重复的数据](https://leetcode.cn/problems/find-all-duplicates-in-an-array/)|[JavaScript](./algorithms/find-all-duplicates-in-an-array.js)|Medium|

algorithms/longest-palindrome.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 409. 最长回文串
3+
* @param {string} s
4+
* @return {number}
5+
*/
6+
var longestPalindrome = function (s) {
7+
let count = 0;
8+
const map = Array(52).fill(0);
9+
10+
for (let i = 0; i < s.length; i++) {
11+
// lower letter: 0 ~ 25
12+
// upper letter: 26 ~ 51
13+
let index;
14+
if (s[i] < "a") {
15+
// upper letter
16+
index = s[i].charCodeAt() - 65 + 26;
17+
} else {
18+
index = s[i].charCodeAt() - 97;
19+
}
20+
if (map[index]) {
21+
// 成双成对
22+
count += 2;
23+
map[index]--;
24+
} else {
25+
map[index]++;
26+
}
27+
}
28+
for (let i = 0; i < map.length; i++) {
29+
if (map[i] === 1) {
30+
count++;
31+
break;
32+
}
33+
}
34+
return count;
35+
};

0 commit comments

Comments
 (0)