Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite26aee5

Browse files
refactor 516
1 parent353dc7b commite26aee5

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,26 @@
2222
*/
2323
publicclass_516 {
2424

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-
publicintlongestPalindromeSubseq(Strings) {
32-
int[][]dp =newint[s.length()][s.length()];
33-
for (inti =s.length() -1;i >=0;i--) {
34-
dp[i][i] =1;//initialization
35-
for (intj =i +1;j <s.length();j++) {
36-
if (s.charAt(i) ==s.charAt(j)) {
37-
dp[i][j] =dp[i +1][j -1] +2;
38-
}else {
39-
dp[i][j] =Math.max(dp[i +1][j],dp[i][j -1]);
25+
publicstaticclassSolution1 {
26+
/**
27+
* Inspired by https://discuss.leetcode.com/topic/78603/straight-forward-java-dp-solution
28+
* dp[i][j] means the longest palindromic subsequence's length of substring(i, j)
29+
* so, in the end, we return dp[0][s.length() - 1] which means the longest palindromic subsequence
30+
* of this whole string.
31+
*/
32+
publicintlongestPalindromeSubseq(Strings) {
33+
int[][]dp =newint[s.length()][s.length()];
34+
for (inti =s.length() -1;i >=0;i--) {
35+
dp[i][i] =1;//initialization
36+
for (intj =i +1;j <s.length();j++) {
37+
if (s.charAt(i) ==s.charAt(j)) {
38+
dp[i][j] =dp[i +1][j -1] +2;
39+
}else {
40+
dp[i][j] =Math.max(dp[i +1][j],dp[i][j -1]);
41+
}
4042
}
4143
}
44+
returndp[0][s.length() -1];
4245
}
43-
returndp[0][s.length() -1];
4446
}
45-
4647
}

‎src/test/java/com/fishercoder/_516Test.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
importorg.junit.BeforeClass;
55
importorg.junit.Test;
66

7-
importstaticjunit.framework.Assert.assertEquals;
7+
importstaticorg.junit.Assert.assertEquals;
88

99
publicclass_516Test {
10-
privatestatic_516test;
10+
privatestatic_516.Solution1solution1;
1111

1212
@BeforeClass
1313
publicstaticvoidsetup() {
14-
test =new_516();
14+
solution1 =new_516.Solution1();
1515
}
1616

1717
@Test
1818
publicvoidtest1() {
19-
assertEquals(4,test.longestPalindromeSubseq("bbbab"));
19+
assertEquals(4,solution1.longestPalindromeSubseq("bbbab"));
2020
}
2121

2222
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp