2022.2.12—–leetcode.1020
public static int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};//四个方向
private int m, n;
private boolean[][] marked;//某格是否搜索过
public int numEnclaves(int[][] grid) {
m = grid.length;
n = grid[0].length;
marked = new boolean[m][n];
//从四条边开始搜索,能搜索到的一定不是飞地
for (int i = 0; i < m; i++) {
dfs(grid, i, 0);
dfs(grid, i, n - 1);
}
for (int j = 1; j < n - 1; j++) {
dfs(grid, 0, j);
共有 0 条评论