File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 134
134
| 396| [ 旋转函数] ( https://leetcode.cn/problems/rotate-function/ ) | [ JavaScript] ( ./algorithms/rotate-function.js ) | Medium|
135
135
| 404| [ 左叶子之和] ( https://leetcode.cn/problems/sum-of-left-leaves/ ) | [ JavaScript] ( ./algorithms/sum-of-left-leaves.js ) | Easy|
136
136
| 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|
137
138
| 412| [ Fizz Buzz] ( https://leetcode.cn/problems/fizz-buzz/ ) | [ JavaScript] ( ./algorithms/fizz-buzz.js ) | Easy|
138
139
| 414| [ 第三大的数] ( https://leetcode.cn/problems/third-maximum-number/ ) | [ JavaScript] ( ./algorithms/third-maximum-number.js ) | Easy|
139
140
| 442| [ 数组中重复的数据] ( https://leetcode.cn/problems/find-all-duplicates-in-an-array/ ) | [ JavaScript] ( ./algorithms/find-all-duplicates-in-an-array.js ) | Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments