200.Number of Islands (M)
https://leetcode.com/problems/number-of-islands/
1.Description
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]2.Code
Last updated
https://leetcode.com/problems/number-of-islands/
[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]Last updated
private int m,n;
public int numIslands(boolean[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) {
return 0;
}
int result=0;
m=grid.length;
n=grid[0].length;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==true){
result++;
bfs(grid,i,j);
}
}
}
return result;
}
public void bfs(boolean[][] grid,int x,int y){
//first set itself as false
grid[x][y]=false;
LinkedList<Integer> queue=new LinkedList<Integer>();
//transfer two-dimension to one-dimension
int node=x*n+y;
queue.offer(node);
//queue record the "1" and its adjacent node
while(!queue.isEmpty()){
int current=queue.poll();
//switch current to two-dimension
int i=current/n;
int j=current%n;
//check upward if it is "1" set it as "0" and push into queue
if(i>0 && grid[i-1][j]==true){
grid[i-1][j]=false;
queue.offer((i-1)*n+j);
}
//check downward
if(i<m-1 && grid[i+1][j]==true){
grid[i+1][j]=false;
queue.offer((i+1)*n+j);
}
//check left
if(j>0 && grid[i][j-1]==true){
grid[i][j-1]=false;
queue.offer(i*n+j-1);
}
//check right
if(j<n-1 && grid[i][j+1]==true){
grid[i][j+1]=false;
queue.offer(i*n+j+1);
}
}
}class Node { int x; int y;
public Node(int x, int y)
{
this.x = x;
this.y = y;
}
}public class Solution {
public int numIslands(char[][] grid)
{
if(grid == null || grid.length == 0 || grid[0].length == 0)
{
return 0;
}
int height = grid.length;
int length = grid[0].length;
int result = 0;
for(int i= 0; i< height; i++)
{
for(int j = 0; j< length; j++)
{
if(grid[i][j] == '1')
{
bfsNode(grid, i, j);
result++;
}
}
}
return result;
}
public void bfsNode(char[][] grid, int x, int y)
{
int[] directionX = {0, 0, -1, 1};
int[] directionY = {-1, 1, 0, 0};
Queue<Node> queue = new LinkedList<Node>();
Set<Node> visited = new HashSet<Node>();
queue.offer(new Node(x, y));
grid[x][y] = '0';
while(!queue.isEmpty())
{
Node current = queue.poll();
for(int j = 0; j< 4; j++)
{
int next_x = current.x + directionX[j];
int next_y = current.y + directionY[j];
if(inBound(grid, next_x, next_y))
{
Node adjacent = new Node(next_x, next_y);
if(grid[next_x][next_y] == '1' && !visited.contains(adjacent))
{
queue.offer(adjacent);
visited.add(adjacent);
grid[next_x][next_y] = '0';
}
}
}
}
}
public boolean inBound(char[][] grid, int x, int y)
{
int m = grid.length;
int n = grid[0].length;
return x>=0 && y>=0 && x<m && y<n;
}
}// 主函数,计算岛屿数量
int numIslands(char[][] grid) {
int res = 0;
int m = grid.length, n = grid[0].length;
// 遍历 grid
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
// 每发现一个岛屿,岛屿数量加一
res++;
// 然后使用 DFS 将岛屿淹了
dfs(grid, i, j);
}
}
}
return res;
}
// 从 (i, j) 开始,将与之相邻的陆地都变成海水
void dfs(char[][] grid, int i, int j) {
int m = grid.length, n = grid[0].length;
if (i < 0 || j < 0 || i >= m || j >= n) {
// 超出索引边界
return;
}
if (grid[i][j] == '0') {
// 已经是海水了
return;
}
// 将 (i, j) 变成海水
grid[i][j] = '0';
// 淹没上下左右的陆地
dfs(grid, i + 1, j);
dfs(grid, i, j + 1);
dfs(grid, i - 1, j);
dfs(grid, i, j - 1);
}