Skip to content

Commit 4bbc611

Browse files
committed
一刷516
1 parent 198c158 commit 4bbc611

File tree

7 files changed

+98
-35
lines changed

7 files changed

+98
-35
lines changed

Diff for: README.adoc

+7-7
Original file line numberDiff line numberDiff line change
@@ -3638,13 +3638,13 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
36383638
|Medium
36393639
|层序遍历
36403640

3641-
//|{counter:codes}
3642-
//|{leetcode_base_url}/longest-palindromic-subsequence/[516. Longest Palindromic Subsequence^]
3643-
//|{source_base_url}/_0516_LongestPalindromicSubsequence.java[Java]
3644-
//|{doc_base_url}/0516-longest-palindromic-subsequence.adoc[题解]
3645-
//|Medium
3646-
//|
3647-
//
3641+
|{counter:codes}
3642+
|{leetcode_base_url}/longest-palindromic-subsequence/[516. Longest Palindromic Subsequence^]
3643+
|{source_base_url}/_0516_LongestPalindromicSubsequence.java[Java]
3644+
|{doc_base_url}/0516-longest-palindromic-subsequence.adoc[题解]
3645+
|Medium
3646+
|
3647+
36483648
//|{counter:codes}
36493649
//|{leetcode_base_url}/super-washing-machines/[517. Super Washing Machines^]
36503650
//|{source_base_url}/_0517_SuperWashingMachines.java[Java]

Diff for: docs/0516-longest-palindromic-subsequence.adoc

+51-27
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,68 @@
11
[#0516-longest-palindromic-subsequence]
2-
= 516. Longest Palindromic Subsequence
2+
= 516. 最长回文子序列
33

4-
{leetcode}/problems/longest-palindromic-subsequence/[LeetCode - Longest Palindromic Subsequence^]
4+
https://leetcode.cn/problems/longest-palindromic-subsequence/[LeetCode - 516. 最长回文子序列 ^]
55

6+
给你一个字符串 `s` ,找出其中最长的回文子序列,并返回该序列的长度。
67

7-
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
8+
子序列定义为:不改变剩余字符顺序的情况下,删除某些字符或者不删除任何字符形成的一个序列。
89

10+
*示例 1:*
911

10-
*Example 1:*
11-
Input:
12-
[subs="verbatim,quotes,macros"]
13-
----
14-
"bbbab"
15-
----
16-
Output:
17-
[subs="verbatim,quotes,macros"]
18-
----
19-
4
20-
----
21-
One possible longest palindromic subsequence is "bbbb".
12+
....
13+
输入:s = "bbbab"
14+
输出:4
15+
解释:一个可能的最长回文子序列为 "bbbb" 。
16+
....
2217

18+
*示例 2:*
2319

24-
*Example 2:*
25-
Input:
26-
[subs="verbatim,quotes,macros"]
27-
----
28-
"cbbd"
29-
----
30-
Output:
31-
[subs="verbatim,quotes,macros"]
32-
----
33-
2
34-
----
35-
One possible longest palindromic subsequence is "bb".
20+
....
21+
输入:s = "cbbd"
22+
输出:2
23+
解释:一个可能的最长回文子序列为 "bb" 。
24+
....
25+
26+
*提示:*
3627

28+
* `+1 <= s.length <= 1000+`
29+
* `s` 仅由小写英文字母组成
3730
3831
32+
== 思路分析
33+
34+
在推演转移方程时,一个很大的误区是从 `dp` 矩阵视角去看。因为这是处理字符串,从一维字符串字符坐标去看,会更加清楚明了。如下图:
35+
36+
image::images/0516-01.png[{image_attr}]
37+
38+
根据代码流程来看,整个处理流程是从字符串的右边向左处理,相当于在“结尾”中寻找回文子串。所以,相关遍历只涉及的矩阵的左上方一半,从最下一行向上,逐行遍历。
39+
3940
[[src-0516]]
41+
[tabs]
42+
====
43+
一刷::
44+
+
45+
--
4046
[{java_src_attr}]
4147
----
4248
include::{sourcedir}/_0516_LongestPalindromicSubsequence.java[tag=answer]
4349
----
50+
--
51+
52+
// 二刷::
53+
// +
54+
// --
55+
// [{java_src_attr}]
56+
// ----
57+
// include::{sourcedir}/_0516_LongestPalindromicSubsequence_2.java[tag=answer]
58+
// ----
59+
// --
60+
====
61+
62+
63+
== 参考资料
64+
65+
. https://leetcode.cn/problems/longest-palindromic-subsequence/solutions/930442/zui-chang-hui-wen-zi-xu-lie-by-leetcode-hcjqp/[516. 最长回文子序列 - 官方题解^]
66+
. https://leetcode.cn/problems/longest-palindromic-subsequence/solutions/15118/dong-tai-gui-hua-si-yao-su-by-a380922457-3/comments/230465/[516. 最长回文子序列 - 动态规划,四要素^] -- 该评论详细解释了如何处理奇偶回文的。
67+
4468

Diff for: docs/images/0516-01.png

29.9 KB
Loading

Diff for: docs/index.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ include::0513-find-bottom-left-tree-value.adoc[leveloffset=+1]
11141114

11151115
include::0515-find-largest-value-in-each-tree-row.adoc[leveloffset=+1]
11161116

1117-
// include::0516-longest-palindromic-subsequence.adoc[leveloffset=+1]
1117+
include::0516-longest-palindromic-subsequence.adoc[leveloffset=+1]
11181118

11191119
// include::0517-super-washing-machines.adoc[leveloffset=+1]
11201120

Diff for: logbook/202503.adoc

+5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ endif::[]
350350
|{doc_base_url}/0140-word-break-ii.adoc[题解]
351351
|✅ 回溯。没想到从 `LinkedList` 切换到 `ArrayList`,内存占用就大幅下降 43.77% → 91.82%。没有使用备忘录耗时已经击败了 98.49%。
352352

353+
|{counter:codes2503}
354+
|{leetcode_base_url}/longest-palindromic-subsequence/[516. 最长回文子序列^]
355+
|{doc_base_url}/0516-longest-palindromic-subsequence.adoc[题解]
356+
|❌ 动态规划。相当于在“结尾”中寻找回文子串。从一维字符串字符坐标去看转移方程,会更加清楚明了。
357+
353358
|===
354359

355360
截止目前,本轮练习一共完成 {codes2503} 道题。

Diff for: src/main/java/com/diguage/algo/leetcode/_0515_FindLargestValueInEachTreeRow.java

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
*/
1111
public class _0515_FindLargestValueInEachTreeRow {
1212
// tag::answer[]
13+
/**
14+
* @author D瓜哥 · https://www.diguage.com
15+
* @since 2024-06-25 15:32:45
16+
*/
1317
public List<Integer> largestValues(TreeNode root) {
1418
if (root == null) {
1519
return Collections.emptyList();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _0516_LongestPalindromicSubsequence {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-04-19 21:32:01
8+
*/
9+
public int longestPalindromeSubseq(String s) {
10+
int length = s.length();
11+
int[][] dp = new int[length][length];
12+
for (int i = length - 1; i >= 0; i--) {
13+
dp[i][i] = 1;
14+
char ic = s.charAt(i);
15+
for (int j = i + 1; j < length; j++) {
16+
char jc = s.charAt(j);
17+
if (ic == jc) {
18+
dp[i][j] = dp[i + 1][j - 1] + 2;
19+
} else {
20+
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
21+
}
22+
}
23+
}
24+
return dp[0][length - 1];
25+
}
26+
// end::answer[]
27+
public static void main(String[] args) {
28+
new _0516_LongestPalindromicSubsequence().longestPalindromeSubseq("ababa");
29+
}
30+
}

0 commit comments

Comments
 (0)