File tree Expand file tree Collapse file tree 2 files changed +31
-8
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 3
3
/**
4
4
* 516. Longest Palindromic Subsequence
5
5
*
6
- * Given a string s, find the longest palindromic subsequence's length in s. You may assume that the maximum length of s is 1000.
6
+ * Given a string s, find the longest palindromic subsequence's length in s.
7
+ * You may assume that the maximum length of s is 1000.
7
8
8
9
Example 1:
9
10
Input:
21
22
*/
22
23
public class _516 {
23
24
24
- /**Inspired by https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution*/
25
- public static int longestPalindromeSubseq (String s ) {
25
+ /**
26
+ * Inspired by https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution
27
+ * dp[i][j] means the longest palindromic subsequence's length of substring(i, j)
28
+ * so, in the end, we return dp[0][s.length() - 1] which means the longest palindromic subsequence
29
+ * of this whole string.
30
+ */
31
+ public int longestPalindromeSubseq (String s ) {
26
32
int [][] dp = new int [s .length ()][s .length ()];
27
33
for (int i = s .length () - 1 ; i >= 0 ; i --) {
28
34
dp [i ][i ] = 1 ;//initialization
@@ -37,9 +43,4 @@ public static int longestPalindromeSubseq(String s) {
37
43
return dp [0 ][s .length () - 1 ];
38
44
}
39
45
40
- public static void main (String ... args ) {
41
- longestPalindromeSubseq ("bbbab" );
42
- System .out .println ("Done." );
43
- }
44
-
45
46
}
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._516 ;
4
+ import org .junit .BeforeClass ;
5
+ import org .junit .Test ;
6
+
7
+ import static junit .framework .Assert .assertEquals ;
8
+
9
+ public class _516Test {
10
+ private static _516 test ;
11
+
12
+ @ BeforeClass
13
+ public static void setup () {
14
+ test = new _516 ();
15
+ }
16
+
17
+ @ Test
18
+ public void test1 () {
19
+ assertEquals (4 , test .longestPalindromeSubseq ("bbbab" ));
20
+ }
21
+
22
+ }
You can’t perform that action at this time.
0 commit comments