Skip to content

Commit 128524e

Browse files
refactor 516
1 parent e04a7a2 commit 128524e

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

src/main/java/com/fishercoder/solutions/_516.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/**
44
* 516. Longest Palindromic Subsequence
55
*
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.
78
89
Example 1:
910
Input:
@@ -21,8 +22,13 @@
2122
*/
2223
public class _516 {
2324

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) {
2632
int[][] dp = new int[s.length()][s.length()];
2733
for (int i = s.length() - 1; i >= 0; i--) {
2834
dp[i][i] = 1;//initialization
@@ -37,9 +43,4 @@ public static int longestPalindromeSubseq(String s) {
3743
return dp[0][s.length() - 1];
3844
}
3945

40-
public static void main(String... args) {
41-
longestPalindromeSubseq("bbbab");
42-
System.out.println("Done.");
43-
}
44-
4546
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)