注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
以下是我的代码,有test没有通过。有没有大神可以帮忙debug一下?我被困了很久还是没有debug成功...提供帮助的都加米!!!谢谢!!!
class Coordinate {
int x, y;
Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
}
class Solution {
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0 || grid[0].length == 0) return 0;
int n = grid.length;
int m = grid[0].length;
int islands = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(grid[i][j] != 0) {
judgeIslands(grid, i, j);
islands++;
}
}
}
return islands;
}
public void judgeIslands(char[][] grid, int x, int y) {
// (1, 0): go right; (0, 1): go down; (0, -1): go up; (-1, 0): go left.
int[] directionX = {1, 0, 0, -1};
int[] directionY = {0, 1, -1, 0};
Queue<Coordinate> queue = new LinkedList<>();
queue.offer(new Coordinate(x, y));
grid[x][y] = 0;
while(!queue.isEmpty()) {
Coordinate coor = queue.poll();
for(int i = 0; i < 4; i++) {
Coordinate adj = new Coordinate(coor.x + directionX[i], coor.y + directionY[i]);
if(!inBound(grid, adj)) continue;
if(grid[adj.x][adj.y] != 0) {
queue.offer(adj);
grid[adj.x][adj.y] = 0;
}
}
}
}
public boolean inBound(char[][] grid, Coordinate coor) {
int n = grid.length;
int m = grid[0].length;
return coor.x >= 0 && coor.x < n && coor.y >= 0 && coor.y < m;
}
} |