modify code

pull/6/head
左程云 5 years ago
parent 3e511ede53
commit bc1eb5780b

@ -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<Integer> 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<List<Integer>> 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<Integer> 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<Integer> 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("测试结束");
}
}

@ -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<Integer> stack = new Stack<Integer>();
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);

Loading…
Cancel
Save