modify code

pull/6/head
左程云 4 years ago
parent 9b66c59d72
commit 10f70fbe68

@ -1,78 +0,0 @@
package class18;
public class Code03_Knapsack {
public static int getMaxValue(int[] w, int[] v, int bag) {
return process(w, v, 0, 0, bag);
}
// index... 最大价值
public static int process(int[] w, int[] v, int index, int alreadyW, int bag) {
if (alreadyW > bag) {
return -1;
}
// 重量没超
if (index == w.length) {
return 0;
}
int p1 = process(w, v, index + 1, alreadyW, bag);
int p2next = process(w, v, index + 1, alreadyW + w[index], bag);
int p2 = -1;
if (p2next != -1) {
p2 = v[index] + p2next;
}
return Math.max(p1, p2);
}
public static int maxValue(int[] w, int[] v, int bag) {
return process(w, v, 0, bag);
}
// 只剩下rest的空间了
// index...货物自由选择但是不要超过rest的空间
// 返回能够获得的最大价值
public static int process(int[] w, int[] v, int index, int rest) {
if (rest < 0) { // base case 1
return -1;
}
// rest >=0
if (index == w.length) { // base case 2
return 0;
}
// 有货也有空间
int p1 = process(w, v, index + 1, rest);
int p2 = -1;
int p2Next = process(w, v, index + 1, rest - w[index]);
if (p2Next != -1) {
p2 = v[index] + p2Next;
}
return Math.max(p1, p2);
}
public static int dpWay(int[] w, int[] v, int bag) {
int N = w.length;
int[][] dp = new int[N + 1][bag + 1];
// dp[N][...] = 0
for (int index = N - 1; index >= 0; index--) {
for (int rest = 0; rest <= bag; rest++) { // rest < 0
int p1 = dp[index + 1][rest];
int p2 = -1;
if (rest - w[index] >= 0) {
p2 = v[index] + dp[index + 1][rest - w[index]];
}
dp[index][rest] = Math.max(p1, p2);
}
}
return dp[0][bag];
}
public static void main(String[] args) {
int[] weights = { 3, 2, 4, 7 };
int[] values = { 5, 6, 3, 19 };
int bag = 11;
System.out.println(maxValue(weights, values, bag));
System.out.println(dpWay(weights, values, bag));
}
}

@ -1,69 +0,0 @@
package class18;
public class Code04_ConvertToLetterString {
public static int number(String str) {
if (str == null || str.length() == 0) {
return 0;
}
return process(str.toCharArray(), 0);
}
public static int process(char[] str, int i) {
if (i == str.length) {
return 1;
}
if (str[i] == '0') {
return 0;
}
if (str[i] == '1') {
int res = process(str, i + 1);
if (i + 1 < str.length) {
res += process(str, i + 2);
}
return res;
}
if (str[i] == '2') {
int res = process(str, i + 1);
if (i + 1 < str.length && (str[i + 1] >= '0' && str[i + 1] <= '6')) {
res += process(str, i + 2); // (i和i+1)作为单独的部分,后续有多少种方法
}
return res;
}
return process(str, i + 1);
}
public static int dp(String s) {
if (s == null || s.length() == 0) {
return 0;
}
char[] str = s.toCharArray();
int N = str.length;
int[] dp = new int[N + 1];
dp[N] = 1;
for (int i = N - 1; i >= 0; i--) {
if (str[i] == '0') {
dp[i] = 0;
} else if (str[i] == '1') {
dp[i] = dp[i + 1];
if (i + 1 < N) {
dp[i] += dp[i + 2];
}
} else if (str[i] == '2') {
dp[i] = dp[i + 1];
if (i + 1 < str.length && (str[i + 1] >= '0' && str[i + 1] <= '6')) {
dp[i] += dp[i + 2];
}
} else {
dp[i] = dp[i + 1];
}
}
return dp[0];
}
public static void main(String[] args) {
System.out.println(number("2132082"));
System.out.println(dp("2132082"));
}
}

@ -0,0 +1,63 @@
package class19;
public class Code01_Knapsack {
// 所有的货重量和价值都在w和v数组里
// 为了方便,其中没有负数
// bag背包容量不能超过这个载重
// 返回:不超重的情况下,能够得到的最大价值
public static int maxValue(int[] w, int[] v, int bag) {
if (w == null || v == null || w.length != v.length || w.length == 0) {
return 0;
}
// 尝试函数!
return process(w, v, 0, bag);
}
// index 0~N
// rest 负~bag
public static int process(int[] w, int[] v, int index, int rest) {
if (rest < 0) {
return -1;
}
if (index == w.length) {
return 0;
}
int p1 = process(w, v, index + 1, rest);
int p2 = 0;
int next = process(w, v, index + 1, rest - w[index]);
if (next != -1) {
p2 = v[index] + next;
}
return Math.max(p1, p2);
}
public static int dp(int[] w, int[] v, int bag) {
if (w == null || v == null || w.length != v.length || w.length == 0) {
return 0;
}
int N = w.length;
int[][] dp = new int[N + 1][bag + 1];
for (int index = N - 1; index >= 0; index--) {
for (int rest = 0; rest <= bag; rest++) {
int p1 = dp[index + 1][rest];
int p2 = 0;
int next = rest - w[index] < 0 ? -1 : dp[index + 1][rest - w[index]];
if (next != -1) {
p2 = v[index] + next;
}
dp[index][rest] = Math.max(p1, p2);
}
}
return dp[0][bag];
}
public static void main(String[] args) {
int[] weights = { 3, 2, 4, 7, 3, 1, 7 };
int[] values = { 5, 6, 3, 19, 12, 4, 2 };
int bag = 15;
System.out.println(maxValue(weights, values, bag));
System.out.println(dp(weights, values, bag));
}
}

@ -1,58 +0,0 @@
package class19;
// 这个问题leetcode上可以直接测
// 链接https://leetcode.com/problems/longest-common-subsequence/
public class Code01_LongestCommonSubsequence {
public static int longestCommonSubsequence1(String text1, String text2) {
if (text1 == null || text2 == null || text1.length() == 0 || text2.length() == 0) {
return 0;
}
return process(text1.toCharArray(), text2.toCharArray(), text1.length() - 1, text2.length() - 1);
}
// str1[0..i]与str2[0..j]最长公共子序列多长
public static int process(char[] str1, char[] str2, int i, int j) {
if (i == 0 && j == 0) {
return str1[0] == str2[0] ? 1 : 0;
}
if (i == 0) {
return str1[0] == str2[j] ? 1 : process(str1, str2, 0, j - 1);
}
if (j == 0) {
return str1[i] == str2[0] ? 1 : process(str1, str2, i - 1, 0);
}
int p1 = process(str1, str2, i - 1, j);
int p2 = process(str1, str2, i, j - 1);
int p3 = str1[i] == str2[j] ? (process(str1, str2, i - 1, j - 1) + 1) : 0;
return Math.max(Math.max(p1, p2), p3);
}
public static int longestCommonSubsequence2(String text1, String text2) {
if (text1 == null || text2 == null || text1.length() == 0 || text2.length() == 0) {
return 0;
}
char[] str1 = text1.toCharArray();
char[] str2 = text2.toCharArray();
int N = str1.length;
int M = str2.length;
int[][] dp = new int[N][M];
dp[0][0] = str1[0] == str2[0] ? 1 : 0;
for (int i = 1; i < N; i++) {
dp[i][0] = str1[i] == str2[0] ? 1 : dp[i - 1][0];
}
for (int j = 1; j < M; j++) {
dp[0][j] = str1[0] == str2[j] ? 1 : dp[0][j - 1];
}
for (int i = 1; i < N; i++) {
for (int j = 1; j < M; j++) {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
if (str1[i] == str2[j]) {
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
}
return dp[N - 1][M - 1];
}
}

@ -0,0 +1,58 @@
package class19;
public class Code02_ConvertToLetterString {
// str只含有数字字符0~9
// 返回多少种转化方案
public static int number(String str) {
if (str == null || str.length() == 0) {
return 0;
}
return process(str.toCharArray(), 0);
}
// str[0..i-1]转化无需过问
// str[i.....]去转化,返回有多少种转化方法
public static int process(char[] str, int i) {
if (i == str.length) {
return 1;
}
// i没到最后说明有字符
if (str[i] == '0') { // 之前的决定有问题
return 0;
}
// str[i] != '0'
// 可能性一i单转
int ways = process(str, i + 1);
if (i + 1 < str.length && (str[i] - '0') * 10 + str[i + 1] - '0' < 27) {
ways += process(str, i + 2);
}
return ways;
}
public static int dp(String s) {
if (s == null || s.length() == 0) {
return 0;
}
char[] str = s.toCharArray();
int N = str.length;
int[] dp = new int[N + 1];
dp[N] = 1;
for (int i = N - 1; i >= 0; i--) {
if (str[i] != '0') {
int ways = dp[i + 1];
if (i + 1 < str.length && (str[i] - '0') * 10 + str[i + 1] - '0' < 27) {
ways += dp[i + 2];
}
dp[i] = ways;
}
}
return dp[0];
}
public static void main(String[] args) {
System.out.println(number("7210231231232031203123"));
System.out.println(dp("7210231231232031203123"));
}
}

@ -1,15 +1,18 @@
package class18; package class19;
import java.util.HashMap; import java.util.HashMap;
// 本题测试链接https://leetcode.com/problems/stickers-to-spell-word // 本题测试链接https://leetcode.com/problems/stickers-to-spell-word
public class Code05_StickersToSpellWord { public class Code03_StickersToSpellWord {
public static int minStickers1(String[] stickers, String target) { public static int minStickers1(String[] stickers, String target) {
int ans = process1(stickers, target); int ans = process1(stickers, target);
return ans == Integer.MAX_VALUE ? -1 : ans; return ans == Integer.MAX_VALUE ? -1 : ans;
} }
// 所有贴纸stickers每一种贴纸都有无穷张
// target
// 最少张数
public static int process1(String[] stickers, String target) { public static int process1(String[] stickers, String target) {
if (target.length() == 0) { if (target.length() == 0) {
return 0; return 0;
@ -59,10 +62,17 @@ public class Code05_StickersToSpellWord {
return ans == Integer.MAX_VALUE ? -1 : ans; return ans == Integer.MAX_VALUE ? -1 : ans;
} }
// stickers[i] 数组当初i号贴纸的字符统计 int[][] stickers -> 所有的贴纸
// 每一种贴纸都有无穷张
// 返回搞定target的最少张数
// 最少张数
public static int process2(int[][] stickers, String t) { public static int process2(int[][] stickers, String t) {
if (t.length() == 0) { if (t.length() == 0) {
return 0; return 0;
} }
// target做出词频统计
// target aabbc 2 2 1..
// 0 1 2..
char[] target = t.toCharArray(); char[] target = t.toCharArray();
int[] tcounts = new int[26]; int[] tcounts = new int[26];
for (char cha : target) { for (char cha : target) {
@ -71,6 +81,7 @@ public class Code05_StickersToSpellWord {
int N = stickers.length; int N = stickers.length;
int min = Integer.MAX_VALUE; int min = Integer.MAX_VALUE;
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
// 尝试第一张贴纸是谁
int[] sticker = stickers[i]; int[] sticker = stickers[i];
// 最关键的优化(重要的剪枝!这一步也是贪心!) // 最关键的优化(重要的剪枝!这一步也是贪心!)
if (sticker[target[0] - 'a'] > 0) { if (sticker[target[0] - 'a'] > 0) {

@ -0,0 +1,67 @@
package class19;
// 这个问题leetcode上可以直接测
// 链接https://leetcode.com/problems/longest-common-subsequence/
public class Code04_LongestCommonSubsequence {
public static int longestCommonSubsequence1(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) {
return 0;
}
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
// 尝试
return process1(str1, str2, str1.length - 1, str2.length - 1);
}
public static int process1(char[] str1, char[] str2, int i, int j) {
if (i == 0 && j == 0) {
return str1[i] == str2[j] ? 1 : 0;
} else if (i == 0) {
if (str1[i] == str2[j]) {
return 1;
} else {
return process1(str1, str2, i, j - 1);
}
} else if (j == 0) {
if (str1[i] == str2[j]) {
return 1;
} else {
return process1(str1, str2, i - 1, j);
}
} else { // i != 0 && j != 0
int p1 = process1(str1, str2, i - 1, j);
int p2 = process1(str1, str2, i, j - 1);
int p3 = str1[i] == str2[j] ? (1 + process1(str1, str2, i - 1, j - 1)) : 0;
return Math.max(p1, Math.max(p2, p3));
}
}
public static int longestCommonSubsequence2(String s1, String s2) {
if (s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) {
return 0;
}
char[] str1 = s1.toCharArray();
char[] str2 = s2.toCharArray();
int N = str1.length;
int M = str2.length;
int[][] dp = new int[N][M];
dp[0][0] = str1[0] == str2[0] ? 1 : 0;
for (int j = 1; j < M; j++) {
dp[0][j] = str1[0] == str2[j] ? 1 : dp[0][j - 1];
}
for (int i = 1; i < N; i++) {
dp[i][0] = str1[i] == str2[0] ? 1 : dp[i - 1][0];
}
for (int i = 1; i < N; i++) {
for (int j = 1; j < M; j++) {
int p1 = dp[i - 1][j];
int p2 = dp[i][j - 1];
int p3 = str1[i] == str2[j] ? (1 + dp[i - 1][j - 1]) : 0;
dp[i][j] = Math.max(p1, Math.max(p2, p3));
}
}
return dp[N - 1][M - 1];
}
}

@ -1,7 +1,7 @@
package class19; package class20;
// 测试链接https://leetcode.com/problems/longest-palindromic-subsequence/ // 测试链接https://leetcode.com/problems/longest-palindromic-subsequence/
public class Code02_PalindromeSubsequence { public class Code01_PalindromeSubsequence {
public static int longestPalindromeSubseq1(String s) { public static int longestPalindromeSubseq1(String s) {
if (s == null || s.length() == 0) { if (s == null || s.length() == 0) {

@ -1,6 +1,6 @@
package class19; package class20;
public class Code03_HorseJump { public class Code02_HorseJump {
public static int ways(int a, int b, int step) { public static int ways(int a, int b, int step) {
return f(0, 0, step, a, b); return f(0, 0, step, a, b);

@ -1,4 +1,4 @@
package class19; package class20;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -12,7 +12,7 @@ import java.util.PriorityQueue;
// 洗杯子的机器洗完一个杯子时间为a任何一个杯子自然挥发干净的时间为b。 // 洗杯子的机器洗完一个杯子时间为a任何一个杯子自然挥发干净的时间为b。
// 四个参数arr, n, a, b // 四个参数arr, n, a, b
// 假设时间点从0开始返回所有人喝完咖啡并洗完咖啡杯的全部过程结束后至少来到什么时间点。 // 假设时间点从0开始返回所有人喝完咖啡并洗完咖啡杯的全部过程结束后至少来到什么时间点。
public class Code04_Coffee { public class Code03_Coffee {
// 方法一:暴力尝试方法 // 方法一:暴力尝试方法
public static int minTime1(int[] arr, int n, int a, int b) { public static int minTime1(int[] arr, int n, int a, int b) {

@ -1,6 +1,6 @@
package class19; package class20;
public class Code05_MinPathSum { public class Code04_MinPathSum {
public static int minPathSum1(int[][] m) { public static int minPathSum1(int[][] m) {
if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) { if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) {

@ -1,4 +1,4 @@
package class20; package class21;
public class Code01_CoinsWayEveryPaperDifferent { public class Code01_CoinsWayEveryPaperDifferent {

@ -1,4 +1,4 @@
package class20; package class21;
public class Code02_CoinsWayNoLimit { public class Code02_CoinsWayNoLimit {

@ -1,4 +1,4 @@
package class20; package class21;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;

@ -1,4 +1,4 @@
package class20; package class21;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry; import java.util.Map.Entry;

@ -1,4 +1,4 @@
package class20; package class21;
public class Code05_MinCoinsNoLimit { public class Code05_MinCoinsNoLimit {

@ -1,4 +1,4 @@
package class21; package class22;
public class Code01_BobDie { public class Code01_BobDie {

@ -1,4 +1,4 @@
package class21; package class22;
public class Code02_KillMonster { public class Code02_KillMonster {

@ -1,4 +1,4 @@
package class21; package class22;
import java.util.TreeSet; import java.util.TreeSet;

@ -1,4 +1,4 @@
package class21; package class22;
import java.util.TreeSet; import java.util.TreeSet;

@ -1,4 +1,4 @@
package class21; package class22;
public class Code05_NQueens { public class Code05_NQueens {
Loading…
Cancel
Save