Skip to content

Commit 8f754ac

Browse files
committed
modify code
1 parent 355cdbd commit 8f754ac

12 files changed

+64
-83
lines changed
Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,75 @@
11
package class19;
22

3+
// 测试链接:https://leetcode.com/problems/longest-palindromic-subsequence/
34
public class Code02_PalindromeSubsequence {
45

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];
1431
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];
1934
}
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];
2237
}
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++) {
2540
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
2641
if (str1[i] == str2[j]) {
2742
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
28-
}
43+
}
2944
}
3045
}
31-
return dp[str1.length - 1][str2.length - 1];
46+
return dp[N - 1][M - 1];
3247
}
3348

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];
3673
}
3774

3875
}

src/class19/Code03_HorseJump.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22

33
public class Code03_HorseJump {
44

5-
// 10*9
6-
// 0~9 y
7-
// 0~8 x
85
public static int ways(int a, int b, int step) {
96
return f(0, 0, step, a, b);
107
}
118

12-
// 马在(i,j)位置,还有step步要去跳
13-
// 返回最终来到(a,b)的方法数
149
public static int f(int i, int j, int step, int a, int b) {
1510
if (i < 0 || i > 9 || j < 0 || j > 8) {
1611
return 0;
@@ -31,7 +26,6 @@ public static int f(int i, int j, int step, int a, int b) {
3126

3227

3328
public static int waysdp(int a, int b, int s) {
34-
// (i,j,0~ step)
3529
int[][][] dp = new int[10][9][s+1];
3630
dp[a][b][0] = 1;
3731
for(int step = 1 ; step <= s;step++ ) { // 按层来

src/class20/Code01_CoinsWayEveryPaperDifferent.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package class20;
22

3-
// arr是货币数组,其中的值都是正数,每个值都认为是一张货币,
4-
// 即便是值相同的货币认为每一张都是独立且不同的,
5-
// 返回组成aim的方法数
63
public class Code01_CoinsWayEveryPaperDifferent {
74

85
public static int coinWays(int[] arr, int aim) {
@@ -64,7 +61,7 @@ public static void main(String[] args) {
6461
int aim = (int) (Math.random() * maxValue);
6562
int ans1 = coinWays(arr, aim);
6663
int ans2 = dp(arr, aim);
67-
if (ans1 == ans2) {
64+
if (ans1 != ans2) {
6865
System.out.println("Oops!");
6966
printArray(arr);
7067
System.out.println(aim);

src/class20/Code02_CoinsWayNoLimit.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package class20;
22

3-
// arr是面值数组,其中的值都是正数且无重复值,
4-
// 每张面值可以用无限张,返回组成aim的方法数
53
public class Code02_CoinsWayNoLimit {
64

75
public static int coinsWay(int[] arr, int aim) {

src/class20/Code03_CoinsWaySameValueSamePapper.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import java.util.HashMap;
44
import java.util.Map.Entry;
55

6-
// arr是货币数组,其中的值都是正数,每个值都认为是一张货币,
7-
// 值相同的货币认为每一张都没有不同!
8-
// 返回组成aim的方法数
96
public class Code03_CoinsWaySameValueSamePapper {
107

118
public static class Info {

src/class20/Code04_MinCoinsOnePaper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.util.Map.Entry;
55
import java.util.LinkedList;
66

7-
// arr中的每个值都代表一张钱
8-
// arr中都是正数,aim>=0,返回组成aim的最小张数
97
public class Code04_MinCoinsOnePaper {
108

119
public static int minCoins(int[] arr, int aim) {

src/class20/Code05_MinCoinsNoLimit.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package class20;
22

3-
// arr是面值数组,其中的值都是正数且无重复值,
4-
// 每张面值可以用无限张,返回组成aim的最少货币数
53
public class Code05_MinCoinsNoLimit {
64

75
public static int minCoins(int[] arr, int aim) {

src/class21/Code02_KillMonster.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package class21;
22

3-
// 题目描述看包里KillMonster.jpeg
43
public class Code02_KillMonster {
54

65
public static double right1(int N, int M, int K) {

src/class21/Code03_SplitSumClosed.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import java.util.TreeSet;
44

5-
/*
6-
* 给定一个整型数组arr,请把arr中所有的数分成两个集合,尽量让两个集合的累加和接近
7-
* 返回最接近的情况下,较小集合的累加和(较大集合的累加和一定是所有数累加和减去较小集合的累加和)
8-
* 为了方便起见,假设arr中没有负数,其实也可以有
9-
* 但是处理起来会比较麻烦,而且有没有负数都不影响算法流程的理解
10-
* */
115
public class Code03_SplitSumClosed {
126

137
public static int right(int[] arr) {

src/class21/Code04_SplitSumClosedSizeHalf.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22

33
import java.util.TreeSet;
44

5-
/*
6-
* 给定一个整型数组arr,请把arr中所有的数分成两个集合
7-
* 如果arr长度为偶数,两个集合包含数的个数要一样多
8-
* 如果arr长度为奇数,两个集合包含数的个数必须只差一个
9-
* 请尽量让两个集合的累加和接近
10-
* 返回最接近的情况下,较小集合的累加和(较大集合的累加和一定是所有数累加和减去较小集合的累加和)
11-
* 为了方便起见,假设arr中没有负数,其实也可以有
12-
* 但是处理起来会比较麻烦,而且有没有负数都不影响算法流程的理解
13-
* */
145
public class Code04_SplitSumClosedSizeHalf {
156

167
public static int right(int[] arr) {

0 commit comments

Comments
 (0)