-
Notifications
You must be signed in to change notification settings - Fork 44
/
_516_longestPalindromeSubseq.java
66 lines (62 loc) · 2.17 KB
/
_516_longestPalindromeSubseq.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package pp.arithmetic.leetcode;
/**
* Created by wangpeng on 2018/11/8.
* 516. 最长回文子序列
* <p>
* 给定一个字符串s,找到其中最长的回文子序列。可以假设s的最大长度为1000。
* <p>
* 示例 1:
* 输入:
* <p>
* "bbbab"
* 输出:
* <p>
* 4
* 一个可能的最长回文子序列为 "bbbb"。
* <p>
* 示例 2:
* 输入:
* <p>
* "cbbd"
* 输出:
* <p>
* 2
* 一个可能的最长回文子序列为 "bb"。
*
* @see <a href="https://leetcode-cn.com/problems/longest-palindromic-subsequence/description/">longest-palindromic-subsequence</a>
*/
public class _516_longestPalindromeSubseq {
public static void main(String[] args) {
int l1 = longestPalindromeSubseq("bbbab");
System.out.println(l1);
long start = System.currentTimeMillis();
int l2 = longestPalindromeSubseq("euazbipzncptldueeuechubrcourfpftcebikrxhybkymimgvldiwqvkszfycvqyvtiwfckexmowcxztkfyzqovbtmzpxojfofbvwnncajvrvdbvjhcrameamcfmcoxryjukhpljwszknhiypvyskmsujkuggpztltpgoczafmfelahqwjbhxtjmebnymdyxoeodqmvkxittxjnlltmoobsgzdfhismogqfpfhvqnxeuosjqqalvwhsidgiavcatjjgeztrjuoixxxoznklcxolgpuktirmduxdywwlbikaqkqajzbsjvdgjcnbtfksqhquiwnwflkldgdrqrnwmshdpykicozfowmumzeuznolmgjlltypyufpzjpuvucmesnnrwppheizkapovoloneaxpfinaontwtdqsdvzmqlgkdxlbeguackbdkftzbnynmcejtwudocemcfnuzbttcoew");
System.out.println(l2);
long end = System.currentTimeMillis();
System.out.println(end - start);
}
/**
* 动态规划,时间复杂度O(n^2)
*
* @param s
* @return
*/
public static int longestPalindromeSubseq(String s) {
if (s == null || s.length() == 0) {
return 0;
}
//i..j的区间内最大的子序列长度
int[][] dp = new int[s.length()][s.length()];
for (int i = s.length() - 1; i >= 0; i--) {
dp[i][i] = 1;
for (int j = i + 1; j < s.length(); j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i + 1][j - 1] + 2;
} else {
dp[i][j] = Math.max(dp[i + 1][j], dp[i][j - 1]);
}
}
}
return dp[0][s.length() - 1];
}
}