File tree 3 files changed +74
-24
lines changed
src/main/java/com/diguage/algo/leetcode
3 files changed +74
-24
lines changed Original file line number Diff line number Diff line change 1
1
[#0409-longest-palindrome]
2
- = 409. Longest Palindrome
2
+ = 409. 最长回文串
3
3
4
- { leetcode} /problems/longest-palindrome/[LeetCode - Longest Palindrome ^]
4
+ https:// leetcode.cn /problems/longest-palindrome/[LeetCode - 409. 最长回文串 ^]
5
5
6
- Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
6
+ 给定一个包含大写字母和小写字母的字符串 `s` ,返回 _通过这些字母构造成的 *最长的 回文串*_ 的长度。
7
7
8
- This is case sensitive, for example ` "Aa"` is not considered a palindrome here.
8
+ 在构造过程中,请注意 *区分大小写* 。比如 `+ "Aa"+` 不能当做一个回文字符串。
9
9
10
- *Note:*
11
10
11
+ *示例 1:*
12
12
13
- Assume the length of given string will not exceed 1,010.
13
+ ....
14
+ 输入:s = "abccccdd"
15
+ 输出:7
16
+ 解释:
17
+ 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
18
+ ....
14
19
20
+ *示例 2:*
15
21
16
- *Example: *
17
- [subs="verbatim,quotes,macros"]
18
- ----
19
- Input:
20
- "abccccdd"
22
+ ....
23
+ 输入:s = "a"
24
+ 输出:1
25
+ 解释:可以构造的最长回文串是"a",它的长度是 1。
26
+ ....
21
27
22
- Output:
23
- 7
28
+ *提示:*
29
+
30
+ * `+1 <= s.length <= 2000+`
31
+ * `s` 只由小写 *和/或* 大写英文字母组成
24
32
25
- Explanation:
26
- One longest palindrome that can be built is "dccaccd", whose length is 7.
27
- ----
28
33
29
34
== 思路分析
30
35
36
+
31
37
[[src-0409]]
32
38
[tabs]
33
39
====
@@ -40,16 +46,17 @@ include::{sourcedir}/_0409_LongestPalindrome.java[tag=answer]
40
46
----
41
47
--
42
48
43
- // 二刷::
44
- // +
45
- // --
46
- // [{java_src_attr}]
47
- // ----
48
- // include::{sourcedir}/_0409_LongestPalindrome_2.java[tag=answer]
49
- // ----
50
- // --
49
+ 二刷::
50
+ +
51
+ --
52
+ [{java_src_attr}]
53
+ ----
54
+ include::{sourcedir}/_0409_LongestPalindrome_2.java[tag=answer]
55
+ ----
56
+ --
51
57
====
52
58
59
+
53
60
== 参考资料
54
61
55
62
. https://leetcode.cn/problems/longest-palindrome/solutions/156931/zui-chang-hui-wen-chuan-by-leetcode-solution/?envType=study-plan-v2&envId=selected-coding-interview[409. 最长回文串 - 官方题解^]
Original file line number Diff line number Diff line change @@ -673,6 +673,11 @@ endif::[]
673
673
|{doc_base_url} /0392-is-subsequence.adoc[题解]
674
674
|✅ 双指针
675
675
676
+ |{counter:codes2503}
677
+ |{leetcode_base_url} /longest-palindrome/[409. 最长回文串^]
678
+ |{doc_base_url} /0409-longest-palindrome.adoc[题解]
679
+ |✅ 回文
680
+
676
681
|===
677
682
678
683
截止目前,本轮练习一共完成 {codes2503} 道题。
Original file line number Diff line number Diff line change
1
+ package com .diguage .algo .leetcode ;
2
+
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
6
+ public class _0409_LongestPalindrome_2 {
7
+ // tag::answer[]
8
+
9
+ /**
10
+ * @author D瓜哥 · https://www.diguage.com
11
+ * @since 2025-05-13 21:32:00
12
+ */
13
+ public int longestPalindrome (String s ) {
14
+ // 统计每个字符的出现次数
15
+ Map <Character , Integer > map = new HashMap <>();
16
+ for (char c : s .toCharArray ()) {
17
+ map .put (c , map .getOrDefault (c , 0 ) + 1 );
18
+ }
19
+ int result = 0 ;
20
+ for (Integer cnt : map .values ()) {
21
+ if ((cnt & 1 ) == 0 ) {
22
+ // 偶数直接相加
23
+ result += cnt ;
24
+ } else {
25
+ // 奇数需要特殊处理:
26
+ // 当 result 为 0 或者偶数是,直接相加
27
+ // 否则,需要减一
28
+ if ((result & 1 ) == 0 || result == 0 ) {
29
+ result += cnt ;
30
+ }else {
31
+ result += (cnt - 1 );
32
+ }
33
+ }
34
+ }
35
+ return result ;
36
+ }
37
+ // end::answer[]
38
+ }
You can’t perform that action at this time.
0 commit comments