|
1 | 1 | package class19; |
2 | 2 |
|
| 3 | +// 测试链接:https://leetcode.com/problems/longest-palindromic-subsequence/ |
3 | 4 | public class Code02_PalindromeSubsequence { |
4 | 5 |
|
5 | | -public static int lcse(char[] str1, char[] str2) { |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | -int[][] dp = new int[str1.length][str2.length]; |
11 | | - |
12 | | - |
13 | | - |
| 6 | +public static int longestPalindromeSubseq1(String s) { |
| 7 | +if (s == null || s.length() == 0) { |
| 8 | +return 0; |
| 9 | +} |
| 10 | +if (s.length() == 1) { |
| 11 | +return 1; |
| 12 | +} |
| 13 | +char[] str = s.toCharArray(); |
| 14 | +char[] reverse = reverse(str); |
| 15 | +return longestCommonSubsequence(str, reverse); |
| 16 | +} |
| 17 | + |
| 18 | +public static char[] reverse(char[] str) { |
| 19 | +int N = str.length; |
| 20 | +char[] reverse = new char[str.length]; |
| 21 | +for (int i = 0; i < str.length; i++) { |
| 22 | +reverse[--N] = str[i]; |
| 23 | +} |
| 24 | +return reverse; |
| 25 | +} |
| 26 | + |
| 27 | +public static int longestCommonSubsequence(char[] str1, char[] str2) { |
| 28 | +int N = str1.length; |
| 29 | +int M = str2.length; |
| 30 | +int[][] dp = new int[N][M]; |
14 | 31 | dp[0][0] = str1[0] == str2[0] ? 1 : 0; |
15 | | - |
16 | | - |
17 | | -for (int i = 1; i < str1.length; i++) { |
18 | | -dp[i][0] = Math.max(dp[i - 1][0], str1[i] == str2[0] ? 1 : 0); |
| 32 | +for (int i = 1; i < N; i++) { |
| 33 | +dp[i][0] = str1[i] == str2[0] ? 1 : dp[i - 1][0]; |
19 | 34 | } |
20 | | -for (int j = 1; j < str2.length; j++) { |
21 | | -dp[0][j] = Math.max(dp[0][j - 1], str1[0] == str2[j] ? 1 : 0); |
| 35 | +for (int j = 1; j < M; j++) { |
| 36 | +dp[0][j] = str1[0] == str2[j] ? 1 : dp[0][j - 1]; |
22 | 37 | } |
23 | | -for (int i = 1; i < str1.length; i++) { |
24 | | -for (int j = 1; j < str2.length; j++) { |
| 38 | +for (int i = 1; i < N; i++) { |
| 39 | +for (int j = 1; j < M; j++) { |
25 | 40 | dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); |
26 | 41 | if (str1[i] == str2[j]) { |
27 | 42 | dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1); |
28 | | -} |
| 43 | +} |
29 | 44 | } |
30 | 45 | } |
31 | | -return dp[str1.length - 1][str2.length - 1]; |
| 46 | +return dp[N - 1][M - 1]; |
32 | 47 | } |
33 | 48 |
|
34 | | -public static void main(String[] args) { |
35 | | - |
| 49 | +public static int longestPalindromeSubseq2(String s) { |
| 50 | +if (s == null || s.length() == 0) { |
| 51 | +return 0; |
| 52 | +} |
| 53 | +if (s.length() == 1) { |
| 54 | +return 1; |
| 55 | +} |
| 56 | +char[] str = s.toCharArray(); |
| 57 | +int N = str.length; |
| 58 | +int[][] dp = new int[N][N]; |
| 59 | +dp[N - 1][N - 1] = 1; |
| 60 | +for (int i = 0; i < N - 1; i++) { |
| 61 | +dp[i][i] = 1; |
| 62 | +dp[i][i + 1] = str[i] == str[i + 1] ? 2 : 1; |
| 63 | +} |
| 64 | +for (int i = N - 3; i >= 0; i--) { |
| 65 | +for (int j = i + 2; j < N; j++) { |
| 66 | +dp[i][j] = Math.max(dp[i][j - 1], dp[i + 1][j]); |
| 67 | +if (str[i] == str[j]) { |
| 68 | +dp[i][j] = Math.max(dp[i][j], dp[i + 1][j - 1] + 2); |
| 69 | +} |
| 70 | +} |
| 71 | +} |
| 72 | +return dp[0][N - 1]; |
36 | 73 | } |
37 | 74 |
|
38 | 75 | } |
0 commit comments