200. Number of Islands

来源:互联网 发布:非洲军事 知乎 编辑:程序博客网 时间:2024/06/05 19:37

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

11110
110
10
11000
00000 红色的俩1是连着的,看清楚题意

Answer: 1

Example 2:

11000
11000
00100
00011

Answer: 3

public class Solution {    public int numIslands(char[][] grid) {        if(grid == null || grid.length == 0){            return 0;        }        int res = 0;        for(int i=0; i<grid.length; i++){            for(int j=0; j<grid[0].length; j++){                if(grid[i][j] == '1'){                    res++;                    dfs(grid, i, j);                }            }        }        return res;    }        public void dfs(char[][] grid, int i, int j){        if(i<0 || j <0 || i>=grid.length || j>=grid[0].length){            return;        }        if(grid[i][j] == '1'){            grid[i][j] = '0';            dfs(grid, i + 1, j);            dfs(grid, i, j + 1);            dfs(grid, i - 1, j);            dfs(grid, i, j - 1);        }    }}


public class Solution {    public class Node{        int x;        int y;        public Node(int x, int y){            this.x = x;            this.y = y;        }    }        public int numIslands(char[][] grid) {          if(grid == null || grid.length == 0){              return 0;          }          int res = 0;          for(int i=0; i<grid.length; i++){              for(int j=0; j<grid[0].length; j++){                  if(grid[i][j] == '1'){                      res++;                    grid[i][j] = '0';                    Node node = new Node(i, j);                    bfs(grid, node);                  }              }          }          return res;      }        public void bfs(char[][] grid, Node node){        Queue<Node> q = new LinkedList<Node>();        q.offer(node);        while(!q.isEmpty()){            node = q.poll();            if(isValid(grid, node.x-1, node.y) && grid[node.x-1][node.y] =='1'){                q.offer(new Node(node.x-1, node.y));                grid[node.x-1][node.y] = '0';            }            if(isValid(grid, node.x+1, node.y) && grid[node.x+1][node.y] == '1'){                q.offer(new Node(node.x+1, node.y));                grid[node.x+1][node.y] = '0';            }            if(isValid(grid, node.x, node.y-1) && grid[node.x][node.y-1] == '1'){                q.offer(new Node(node.x, node.y-1));                grid[node.x][node.y-1] = '0';            }            if(isValid(grid, node.x, node.y+1) && grid[node.x][node.y+1] == '1'){                q.offer(new Node(node.x, node.y+1));                grid[node.x][node.y+1] = '0';            }        }    }        public boolean isValid(char[][] grid, int x, int y){        int row = grid.length;        int col = grid[0].length;        return x>=0&&y>=0&&x<row&&y<col;    }}


思路:DFS、BFS。只要遍历一遍,碰到一个1,就把它周围所有相连的1都标记为非1,这样整个遍历过程中碰到的1的个数就是所求解。


0 0
原创粉丝点击