diff --git a/src/class40/Code02_LongestSumSubArrayLength.java b/src/class40/Code02_LongestSumSubArrayLength.java index c1a21aa..a89fb14 100644 --- a/src/class40/Code02_LongestSumSubArrayLength.java +++ b/src/class40/Code02_LongestSumSubArrayLength.java @@ -8,6 +8,8 @@ public class Code02_LongestSumSubArrayLength { if (arr == null || arr.length == 0) { return 0; } + // key:前缀和 + // value : 0~value这个前缀和是最早出现key这个值的 HashMap map = new HashMap(); map.put(0, -1); // important int len = 0; diff --git a/src/class40/Code03_LongestLessSumSubArrayLength.java b/src/class40/Code03_LongestLessSumSubArrayLength.java index 5349d2b..c4ebc56 100644 --- a/src/class40/Code03_LongestLessSumSubArrayLength.java +++ b/src/class40/Code03_LongestLessSumSubArrayLength.java @@ -19,12 +19,10 @@ public class Code03_LongestLessSumSubArrayLength { minSumEnds[i] = i; } } + // 迟迟扩不进来那一块儿的开头位置 int end = 0; int sum = 0; - int res = 0; - // i是窗口的最左的位置,end扩出来的最右有效块儿的最后一个位置的,再下一个位置 - // end也是下一块儿的开始位置 - // 窗口:[i~end) + int ans = 0; for (int i = 0; i < arr.length; i++) { // while循环结束之后: // 1) 如果以i开头的情况下,累加和<=k的最长子数组是arr[i..end-1],看看这个子数组长度能不能更新res; @@ -33,14 +31,14 @@ public class Code03_LongestLessSumSubArrayLength { sum += minSums[end]; end = minSumEnds[end] + 1; } - res = Math.max(res, end - i); - if (end > i) { // 窗口内还有数 [i~end) [4,4) + ans = Math.max(ans, end - i); + if (end > i) { // 还有窗口,哪怕窗口没有数字 [i~end) [4,4) sum -= arr[i]; - } else { // 窗口内已经没有数了,说明从i开头的所有子数组累加和都不可能<=k + } else { // i == end, 即将 i++, i > end, 此时窗口概念维持不住了,所以end跟着i一起走 end = i + 1; } } - return res; + return ans; } public static int maxLength(int[] arr, int k) { diff --git a/src/class40/Code07_ZigZagPrintMatrix.java b/src/class40/Code07_ZigZagPrintMatrix.java index 3719878..01b9921 100644 --- a/src/class40/Code07_ZigZagPrintMatrix.java +++ b/src/class40/Code07_ZigZagPrintMatrix.java @@ -21,8 +21,7 @@ public class Code07_ZigZagPrintMatrix { System.out.println(); } - public static void printLevel(int[][] m, int tR, int tC, int dR, int dC, - boolean f) { + public static void printLevel(int[][] m, int tR, int tC, int dR, int dC, boolean f) { if (f) { while (tR != dR + 1) { System.out.print(m[tR++][tC--] + " "); diff --git a/src/class40/Code08_PrintStar.java b/src/class40/Code08_PrintStar.java index be7ade3..77d41ef 100644 --- a/src/class40/Code08_PrintStar.java +++ b/src/class40/Code08_PrintStar.java @@ -40,7 +40,7 @@ public class Code08_PrintStar { } public static void main(String[] args) { - printStar(8); + printStar(5); } }