leetcode更新
This commit is contained in:
parent
c3e7572691
commit
9d72cb9592
|
|
@ -166,4 +166,33 @@ public class LC_1254_ClosedIsland {
|
|||
return find(grid, i + 1, j) & find(grid, i - 1, j) & find(grid, i, j + 1) & find(grid, i, j - 1);
|
||||
}
|
||||
|
||||
|
||||
public int closedIsland4(int[][] grid) {
|
||||
int result = 0;
|
||||
for (int i = 0; i < grid.length; i++) {
|
||||
for (int j = 0; j < grid[0].length; j++) {
|
||||
if (grid[i][j] == 0 && dfs2(grid, i, j)) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private boolean dfs2(int[][] grid, int i, int j) {
|
||||
//碰到1则为封闭岛屿;超出边界则为非封闭岛屿
|
||||
if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length) {
|
||||
return false;
|
||||
}
|
||||
if (grid[i][j] == 1) {
|
||||
return true;
|
||||
}
|
||||
grid[i][j] = 1;
|
||||
|
||||
|
||||
return dfs2(grid, i + 1, j) & dfs2(grid, i, j + 1) & dfs2(grid, i - 1, j) & dfs2(grid, i, j - 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,4 +114,25 @@ public class LC_503_NextGreaterElements {
|
|||
return result;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public int[] nextGreaterElements3(int[] nums) {
|
||||
|
||||
int n = nums.length;
|
||||
ArrayDeque<Integer> stack = new ArrayDeque<>();
|
||||
|
||||
int[] result = new int[n];
|
||||
|
||||
for (int i = n * 2 - 2; i >= 0; i--) {
|
||||
while (!stack.isEmpty() && stack.peek() <= nums[i % n]) {//寻找第一个比当前数大的数
|
||||
stack.pop();
|
||||
}
|
||||
if (i < n) result[i] = stack.isEmpty() ? -1 : stack.peek();
|
||||
stack.push(nums[i % n]);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue