diff --git a/src/class18/Code03_Knapsack.java b/src/class18/Code03_Knapsack.java deleted file mode 100644 index d80c76e..0000000 --- a/src/class18/Code03_Knapsack.java +++ /dev/null @@ -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)); - } - -} diff --git a/src/class18/Code04_ConvertToLetterString.java b/src/class18/Code04_ConvertToLetterString.java deleted file mode 100644 index 99e9825..0000000 --- a/src/class18/Code04_ConvertToLetterString.java +++ /dev/null @@ -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")); - } - -} diff --git a/src/class19/Code01_Knapsack.java b/src/class19/Code01_Knapsack.java new file mode 100644 index 0000000..ab355a2 --- /dev/null +++ b/src/class19/Code01_Knapsack.java @@ -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)); + } + +} diff --git a/src/class19/Code01_LongestCommonSubsequence.java b/src/class19/Code01_LongestCommonSubsequence.java deleted file mode 100644 index 36e94e4..0000000 --- a/src/class19/Code01_LongestCommonSubsequence.java +++ /dev/null @@ -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]; - } - -} diff --git a/src/class19/Code02_ConvertToLetterString.java b/src/class19/Code02_ConvertToLetterString.java new file mode 100644 index 0000000..6b14299 --- /dev/null +++ b/src/class19/Code02_ConvertToLetterString.java @@ -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")); + } + +} diff --git a/src/class18/Code05_StickersToSpellWord.java b/src/class19/Code03_StickersToSpellWord.java similarity index 89% rename from src/class18/Code05_StickersToSpellWord.java rename to src/class19/Code03_StickersToSpellWord.java index fcb375c..90b3a4d 100644 --- a/src/class18/Code05_StickersToSpellWord.java +++ b/src/class19/Code03_StickersToSpellWord.java @@ -1,15 +1,18 @@ -package class18; +package class19; import java.util.HashMap; // 本题测试链接:https://leetcode.com/problems/stickers-to-spell-word -public class Code05_StickersToSpellWord { +public class Code03_StickersToSpellWord { public static int minStickers1(String[] stickers, String target) { int ans = process1(stickers, target); return ans == Integer.MAX_VALUE ? -1 : ans; } + // 所有贴纸stickers,每一种贴纸都有无穷张 + // target + // 最少张数 public static int process1(String[] stickers, String target) { if (target.length() == 0) { return 0; @@ -59,10 +62,17 @@ public class Code05_StickersToSpellWord { return ans == Integer.MAX_VALUE ? -1 : ans; } + // stickers[i] 数组,当初i号贴纸的字符统计 int[][] stickers -> 所有的贴纸 + // 每一种贴纸都有无穷张 + // 返回搞定target的最少张数 + // 最少张数 public static int process2(int[][] stickers, String t) { if (t.length() == 0) { return 0; } + // target做出词频统计 + // target aabbc 2 2 1.. + // 0 1 2.. char[] target = t.toCharArray(); int[] tcounts = new int[26]; for (char cha : target) { @@ -71,6 +81,7 @@ public class Code05_StickersToSpellWord { int N = stickers.length; int min = Integer.MAX_VALUE; for (int i = 0; i < N; i++) { + // 尝试第一张贴纸是谁 int[] sticker = stickers[i]; // 最关键的优化(重要的剪枝!这一步也是贪心!) if (sticker[target[0] - 'a'] > 0) { diff --git a/src/class19/Code04_LongestCommonSubsequence.java b/src/class19/Code04_LongestCommonSubsequence.java new file mode 100644 index 0000000..78862f9 --- /dev/null +++ b/src/class19/Code04_LongestCommonSubsequence.java @@ -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]; + } + +} diff --git a/src/class19/Code02_PalindromeSubsequence.java b/src/class20/Code01_PalindromeSubsequence.java similarity index 96% rename from src/class19/Code02_PalindromeSubsequence.java rename to src/class20/Code01_PalindromeSubsequence.java index 94cf109..062f242 100644 --- a/src/class19/Code02_PalindromeSubsequence.java +++ b/src/class20/Code01_PalindromeSubsequence.java @@ -1,7 +1,7 @@ -package class19; +package class20; // 测试链接:https://leetcode.com/problems/longest-palindromic-subsequence/ -public class Code02_PalindromeSubsequence { +public class Code01_PalindromeSubsequence { public static int longestPalindromeSubseq1(String s) { if (s == null || s.length() == 0) { diff --git a/src/class19/Code03_HorseJump.java b/src/class20/Code02_HorseJump.java similarity index 97% rename from src/class19/Code03_HorseJump.java rename to src/class20/Code02_HorseJump.java index a560edd..4744f1e 100644 --- a/src/class19/Code03_HorseJump.java +++ b/src/class20/Code02_HorseJump.java @@ -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) { return f(0, 0, step, a, b); diff --git a/src/class19/Code04_Coffee.java b/src/class20/Code03_Coffee.java similarity index 99% rename from src/class19/Code04_Coffee.java rename to src/class20/Code03_Coffee.java index fec9a7b..070faa9 100644 --- a/src/class19/Code04_Coffee.java +++ b/src/class20/Code03_Coffee.java @@ -1,4 +1,4 @@ -package class19; +package class20; import java.util.Arrays; import java.util.Comparator; @@ -12,7 +12,7 @@ import java.util.PriorityQueue; // 洗杯子的机器洗完一个杯子时间为a,任何一个杯子自然挥发干净的时间为b。 // 四个参数:arr, n, a, b // 假设时间点从0开始,返回所有人喝完咖啡并洗完咖啡杯的全部过程结束后,至少来到什么时间点。 -public class Code04_Coffee { +public class Code03_Coffee { // 方法一:暴力尝试方法 public static int minTime1(int[] arr, int n, int a, int b) { diff --git a/src/class19/Code05_MinPathSum.java b/src/class20/Code04_MinPathSum.java similarity index 97% rename from src/class19/Code05_MinPathSum.java rename to src/class20/Code04_MinPathSum.java index bc8efeb..80d3e0b 100644 --- a/src/class19/Code05_MinPathSum.java +++ b/src/class20/Code04_MinPathSum.java @@ -1,6 +1,6 @@ -package class19; +package class20; -public class Code05_MinPathSum { +public class Code04_MinPathSum { public static int minPathSum1(int[][] m) { if (m == null || m.length == 0 || m[0] == null || m[0].length == 0) { diff --git a/src/class20/Code01_CoinsWayEveryPaperDifferent.java b/src/class21/Code01_CoinsWayEveryPaperDifferent.java similarity index 99% rename from src/class20/Code01_CoinsWayEveryPaperDifferent.java rename to src/class21/Code01_CoinsWayEveryPaperDifferent.java index 008910d..7df1873 100644 --- a/src/class20/Code01_CoinsWayEveryPaperDifferent.java +++ b/src/class21/Code01_CoinsWayEveryPaperDifferent.java @@ -1,4 +1,4 @@ -package class20; +package class21; public class Code01_CoinsWayEveryPaperDifferent { diff --git a/src/class20/Code02_CoinsWayNoLimit.java b/src/class21/Code02_CoinsWayNoLimit.java similarity index 99% rename from src/class20/Code02_CoinsWayNoLimit.java rename to src/class21/Code02_CoinsWayNoLimit.java index 95dd573..0b3a5a0 100644 --- a/src/class20/Code02_CoinsWayNoLimit.java +++ b/src/class21/Code02_CoinsWayNoLimit.java @@ -1,4 +1,4 @@ -package class20; +package class21; public class Code02_CoinsWayNoLimit { diff --git a/src/class20/Code03_CoinsWaySameValueSamePapper.java b/src/class21/Code03_CoinsWaySameValueSamePapper.java similarity index 99% rename from src/class20/Code03_CoinsWaySameValueSamePapper.java rename to src/class21/Code03_CoinsWaySameValueSamePapper.java index 357fb37..fddbe7e 100644 --- a/src/class20/Code03_CoinsWaySameValueSamePapper.java +++ b/src/class21/Code03_CoinsWaySameValueSamePapper.java @@ -1,4 +1,4 @@ -package class20; +package class21; import java.util.HashMap; import java.util.Map.Entry; diff --git a/src/class20/Code04_MinCoinsOnePaper.java b/src/class21/Code04_MinCoinsOnePaper.java similarity index 99% rename from src/class20/Code04_MinCoinsOnePaper.java rename to src/class21/Code04_MinCoinsOnePaper.java index 02e48a1..7ad8a7e 100644 --- a/src/class20/Code04_MinCoinsOnePaper.java +++ b/src/class21/Code04_MinCoinsOnePaper.java @@ -1,4 +1,4 @@ -package class20; +package class21; import java.util.HashMap; import java.util.Map.Entry; diff --git a/src/class20/Code05_MinCoinsNoLimit.java b/src/class21/Code05_MinCoinsNoLimit.java similarity index 99% rename from src/class20/Code05_MinCoinsNoLimit.java rename to src/class21/Code05_MinCoinsNoLimit.java index b41ad45..31e7c3d 100644 --- a/src/class20/Code05_MinCoinsNoLimit.java +++ b/src/class21/Code05_MinCoinsNoLimit.java @@ -1,4 +1,4 @@ -package class20; +package class21; public class Code05_MinCoinsNoLimit { diff --git a/src/class21/Code01_BobDie.java b/src/class22/Code01_BobDie.java similarity index 98% rename from src/class21/Code01_BobDie.java rename to src/class22/Code01_BobDie.java index 211be20..ba68e3a 100644 --- a/src/class21/Code01_BobDie.java +++ b/src/class22/Code01_BobDie.java @@ -1,4 +1,4 @@ -package class21; +package class22; public class Code01_BobDie { diff --git a/src/class21/Code02_KillMonster.java b/src/class22/Code02_KillMonster.java similarity index 99% rename from src/class21/Code02_KillMonster.java rename to src/class22/Code02_KillMonster.java index fbc2871..3465ab2 100644 --- a/src/class21/Code02_KillMonster.java +++ b/src/class22/Code02_KillMonster.java @@ -1,4 +1,4 @@ -package class21; +package class22; public class Code02_KillMonster { diff --git a/src/class21/Code03_SplitSumClosed.java b/src/class22/Code03_SplitSumClosed.java similarity index 99% rename from src/class21/Code03_SplitSumClosed.java rename to src/class22/Code03_SplitSumClosed.java index c9bb573..85ce147 100644 --- a/src/class21/Code03_SplitSumClosed.java +++ b/src/class22/Code03_SplitSumClosed.java @@ -1,4 +1,4 @@ -package class21; +package class22; import java.util.TreeSet; diff --git a/src/class21/Code04_SplitSumClosedSizeHalf.java b/src/class22/Code04_SplitSumClosedSizeHalf.java similarity index 99% rename from src/class21/Code04_SplitSumClosedSizeHalf.java rename to src/class22/Code04_SplitSumClosedSizeHalf.java index bdfffff..42d4d79 100644 --- a/src/class21/Code04_SplitSumClosedSizeHalf.java +++ b/src/class22/Code04_SplitSumClosedSizeHalf.java @@ -1,4 +1,4 @@ -package class21; +package class22; import java.util.TreeSet; diff --git a/src/class21/Code05_NQueens.java b/src/class22/Code05_NQueens.java similarity index 99% rename from src/class21/Code05_NQueens.java rename to src/class22/Code05_NQueens.java index de49c7a..5bbe7f1 100644 --- a/src/class21/Code05_NQueens.java +++ b/src/class22/Code05_NQueens.java @@ -1,4 +1,4 @@ -package class21; +package class22; public class Code05_NQueens {