Skip to content

Commit a1400b3

Browse files
committed
第六周作业
1 parent 9a15aea commit a1400b3

5 files changed

Lines changed: 129 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Week_06;
2+
3+
public class LongestCommonSubsequence {
4+
public int longestCommonSubsequence(String text1, String text2) {
5+
char[] s1 = text1.toCharArray();
6+
char[] s2 = text2.toCharArray();
7+
8+
int l1 = s1.length + 1;
9+
int l2 = s2.length + 1;
10+
11+
int[][] dp = new int[l1][l2];
12+
13+
for (int i = 1; i < l1; i++) {
14+
for (int j = 1; j < l2; j++) {
15+
if (s1[i - 1] == s2[j - 1]) {
16+
dp[i][j] = dp[i - 1][j - 1] + 1;
17+
} else {
18+
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
19+
}
20+
}
21+
}
22+
return dp[l1 - 1][l2 - 1];
23+
24+
}
25+
}

Week_06/MinimumTotal.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Week_06;
2+
3+
import java.util.List;
4+
5+
public class MinimumTotal {
6+
// 我擅长的动态规划 哈哈
7+
/*public int minimumTotal(List<List<Integer>> triangle) {
8+
int n = triangle.size();
9+
int[] dp = new int[n + 1];
10+
11+
for (int i = n - 1; i >= 0; i--) {
12+
for (int j = 0; j <= i; j++) {
13+
dp[j] = Math.min(dp[j], dp[j + 1]) + triangle.get(i).get(j);
14+
}
15+
}
16+
return dp[0];
17+
}*/
18+
19+
// 我不擅长的递归,尝试老师讲解的方式再写出来
20+
public int minimumTotal(List<List<Integer>> triangle) {
21+
return minimumTotalRec(triangle, 0, 0);
22+
}
23+
24+
private int minimumTotalRec(List<List<Integer>> triangle, int level, int index) {
25+
if (level == triangle.size() - 1) {// 到底层返回对应坐标值即可
26+
return triangle.get(level).get(index);
27+
}
28+
29+
return Math.min(minimumTotalRec(triangle, level + 1, index), minimumTotalRec(triangle, level + 1, index + 1)) + triangle.get(level).get(index);
30+
}
31+
}

Week_06/README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
学习笔记
1+
##学习笔记
2+
3+
1、本周内容主旨:动态规划
4+
动态规划:上完第一节课及做完部分题目我的个人理解是每次走当前分步的最‘捷径’,直至所有分步结合。就是说如果动态规划方案无优劣区分,及一个极端那就是前面我们学过的'分治'.
5+
他比较适合大数据类型的’最小路径和‘题目解法,当前状态和前继最优匹配达成当前最优,整个问题拆分一个个当前节点这样处理,那么所有当前节点合并结果即是动态规划解。
6+
7+
通过这周三天学习及做题情况发现,动态规划至关重要是发现递推公式(数学归纳法)
8+
9+
动态规划我的解法第一步分析题意,定义出合适的状态(一般需要着重考虑到的是空间复杂度),第二步则找出dp方程,dp方程我发现可至上向下顺序滚动,
10+
或者至下向上逆向循环(一般这样也可以递归),而dp方程也有数学归纳法的一点应用,总体根据题目具体分析,下一步则是据此求解处理就可以啦。

Week_06/Rob.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Week_06;
2+
3+
// 打家劫舍(简单)
4+
public class Rob {
5+
public int rob(int[] nums) {
6+
int length = nums.length;
7+
if (length == 0) {
8+
return 0;
9+
}
10+
11+
if (length == 1) {
12+
return nums[0];
13+
}
14+
15+
int resPre = nums[0];
16+
int maxRes = Math.max(resPre, nums[1]);
17+
18+
for (int i = 2; i < length; i++) {
19+
int temp = maxRes;
20+
maxRes = Math.max(maxRes, resPre + nums[i]);
21+
resPre = temp;
22+
}
23+
24+
return maxRes;
25+
26+
}
27+
}

Week_06/Rob2.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Week_06;
2+
3+
public class Rob2 {
4+
public int rob(int[] nums) {
5+
int length = nums.length;
6+
if (length == 0) {
7+
return 0;
8+
}
9+
10+
int ignoreFirst = nums[nums.length - 1];
11+
int ignoreLast = nums[0];
12+
if (length <= 2) {
13+
return Math.max(ignoreFirst, ignoreLast);
14+
}
15+
16+
// 忽略第一个
17+
int resPre = nums[1];
18+
ignoreFirst = Math.max(resPre, nums[2]);
19+
for (int i = 3; i < length; i++) {
20+
int temp = ignoreFirst;
21+
ignoreFirst = Math.max(ignoreFirst, resPre + nums[i]);
22+
resPre = temp;
23+
}
24+
25+
// 忽略最后一个
26+
resPre = nums[0];
27+
ignoreLast = Math.max(resPre, nums[1]);
28+
for (int i = 2; i < length - 1; i++) {
29+
int temp = ignoreLast;
30+
ignoreLast = Math.max(ignoreLast, resPre + nums[i]);
31+
resPre = temp;
32+
}
33+
34+
return Math.max(ignoreFirst, ignoreLast);
35+
}
36+
}

0 commit comments

Comments
 (0)