Skip to content

Commit

Permalink
modify code
Browse files Browse the repository at this point in the history
  • Loading branch information
algorithmzuo committed Mar 18, 2021
1 parent 5a7f8d0 commit 1aeb05c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/class40/Code02_LongestSumSubArrayLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public static int maxLength(int[] arr, int k) {
if (arr == null || arr.length == 0) {
return 0;
}
// key:前缀和
// value : 0~value这个前缀和是最早出现key这个值的
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, -1); // important
int len = 0;
Expand Down
14 changes: 6 additions & 8 deletions src/class40/Code03_LongestLessSumSubArrayLength.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ public static int maxLengthAwesome(int[] arr, int k) {
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;
Expand All @@ -33,14 +31,14 @@ public static int maxLengthAwesome(int[] arr, int k) {
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) {
Expand Down
3 changes: 1 addition & 2 deletions src/class40/Code07_ZigZagPrintMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static void printMatrixZigZag(int[][] matrix) {
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--] + " ");
Expand Down
2 changes: 1 addition & 1 deletion src/class40/Code08_PrintStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static void set(char[][] m, int leftUp, int rightDown) {
}

public static void main(String[] args) {
printStar(8);
printStar(5);
}

}

0 comments on commit 1aeb05c

Please sign in to comment.