From bc1eb5780b88930ca7f6243f7c396987e22055b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B7=A6=E7=A8=8B=E4=BA=91?= Date: Tue, 26 Jan 2021 23:24:06 +0800 Subject: [PATCH] modify code --- src/class25/Code01_MonotonousStack.java | 29 ++++++++++++------- .../Code05_CountSubmatricesWithAllOnes.java | 20 ++++++------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/class25/Code01_MonotonousStack.java b/src/class25/Code01_MonotonousStack.java index c9bb357..b823a30 100644 --- a/src/class25/Code01_MonotonousStack.java +++ b/src/class25/Code01_MonotonousStack.java @@ -6,23 +6,32 @@ import java.util.Stack; public class Code01_MonotonousStack { + // arr = [ 3, 1, 2, 3] + // 0 1 2 3 + // [ + // 0 : [-1, 1] + // 1 : [-1, -1] + // 2 : [ 1, -1] + // 3 : [ 2, -1] + // ] public static int[][] getNearLessNoRepeat(int[] arr) { int[][] res = new int[arr.length][2]; + // 只存位置! Stack stack = new Stack<>(); - for (int i = 0; i < arr.length; i++) { + for (int i = 0; i < arr.length; i++) { // 当遍历到i位置的数,arr[i] while (!stack.isEmpty() && arr[stack.peek()] > arr[i]) { - int popIndex = stack.pop(); + int j = stack.pop(); int leftLessIndex = stack.isEmpty() ? -1 : stack.peek(); - res[popIndex][0] = leftLessIndex; - res[popIndex][1] = i; + res[j][0] = leftLessIndex; + res[j][1] = i; } stack.push(i); } while (!stack.isEmpty()) { - int popIndex = stack.pop(); + int j = stack.pop(); int leftLessIndex = stack.isEmpty() ? -1 : stack.peek(); - res[popIndex][0] = leftLessIndex; - res[popIndex][1] = -1; + res[j][0] = leftLessIndex; + res[j][1] = -1; } return res; } @@ -31,17 +40,14 @@ public class Code01_MonotonousStack { int[][] res = new int[arr.length][2]; Stack> stack = new Stack<>(); for (int i = 0; i < arr.length; i++) { // i -> arr[i] 进栈 - // 底 -> 顶, 小 -> 大 while (!stack.isEmpty() && arr[stack.peek().get(0)] > arr[i]) { List popIs = stack.pop(); - // 取位于下面位置的列表中,最晚加入的那个 int leftLessIndex = stack.isEmpty() ? -1 : stack.peek().get(stack.peek().size() - 1); for (Integer popi : popIs) { res[popi][0] = leftLessIndex; res[popi][1] = i; } } - // 相等的、比你小的 if (!stack.isEmpty() && arr[stack.peek().get(0)] == arr[i]) { stack.peek().add(Integer.valueOf(i)); } else { @@ -52,7 +58,6 @@ public class Code01_MonotonousStack { } while (!stack.isEmpty()) { List popIs = stack.pop(); - // 取位于下面位置的列表中,最晚加入的那个 int leftLessIndex = stack.isEmpty() ? -1 : stack.peek().get(stack.peek().size() - 1); for (Integer popi : popIs) { res[popi][0] = leftLessIndex; @@ -140,6 +145,7 @@ public class Code01_MonotonousStack { int size = 10; int max = 20; int testTimes = 2000000; + System.out.println("测试开始"); for (int i = 0; i < testTimes; i++) { int[] arr1 = getRandomArrayNoRepeat(size); int[] arr2 = getRandomArray(size, max); @@ -154,5 +160,6 @@ public class Code01_MonotonousStack { break; } } + System.out.println("测试结束"); } } diff --git a/src/class25/Code05_CountSubmatricesWithAllOnes.java b/src/class25/Code05_CountSubmatricesWithAllOnes.java index b87f715..ab22814 100644 --- a/src/class25/Code05_CountSubmatricesWithAllOnes.java +++ b/src/class25/Code05_CountSubmatricesWithAllOnes.java @@ -1,7 +1,5 @@ package class25; -import java.util.Stack; - // 测试链接:https://leetcode.com/problems/count-submatrices-with-all-ones public class Code05_CountSubmatricesWithAllOnes { @@ -26,22 +24,24 @@ public class Code05_CountSubmatricesWithAllOnes { return 0; } int nums = 0; - Stack stack = new Stack(); + int[] stack = new int[height.length]; + int si = -1; for (int i = 0; i < height.length; i++) { - while (!stack.isEmpty() && height[stack.peek()] >= height[i]) { - int cur = stack.pop(); + while (si != -1 && height[stack[si]] >= height[i]) { + int cur = stack[si--]; if (height[cur] > height[i]) { - int left = stack.isEmpty() ? -1 : stack.peek(); + int left = si == -1 ? -1 : stack[si]; int n = i - left - 1; int down = Math.max(left == -1 ? 0 : height[left], height[i]); nums += (height[cur] - down) * num(n); } + } - stack.push(i); + stack[++si] = i; } - while (!stack.isEmpty()) { - int cur = stack.pop(); - int left = stack.isEmpty() ? -1 : stack.peek(); + while (si != -1) { + int cur = stack[si--]; + int left = si == -1 ? -1 : stack[si]; int n = height.length - left - 1; int down = left == -1 ? 0 : height[left]; nums += (height[cur] - down) * num(n);