Number of Islands
最新更新的思路,以及题II的解法请访问:https://yanjia.me/zh/2018/11/...
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 11010 11000 00000
Answer: 1
Example 2:
11000 11000 00100 00011
Answer: 3
标零法
复杂度
时间 O(NM) 空间 O(max(N,M)) 递归栈空间
思路
我们遍历矩阵的每一个点,对每个点都尝试进行一次深度优先搜索,如果搜索到1,就继续向它的四周搜索。同时我们每找到一个1,就将其标为0,这样就能把整个岛屿变成0。我们只要记录对矩阵遍历时能进入多少次搜索,就代表有多少个岛屿。
代码
public class Solution {
public int numIslands(char[][] grid) {
int res = 0;
if(grid.length==0) return res;
for(int i = 0; i < grid.length; i++){
for(int j = 0; j < grid[0].length; j++){
if(grid[i][j]=='1'){
searchIsland(grid, i, j);
res++;
}
}
}
return res;
}
private void searchIsland(char[][] grid, int i, int j){
grid[i][j]='0';
// 搜索该点连通的上下左右
if(i>0 && grid[i-1][j]=='1') searchIsland(grid, i-1, j);
if(j>0 && grid[i][j-1]=='1') searchIsland(grid, i, j-1);
if(i<grid.length-1 && grid[i+1][j]=='1') searchIsland(grid, i+1, j);
if(j<grid[0].length-1 && grid[i][j+1]=='1') searchIsland(grid, i, j+1);
}
}
2018/2
class Solution:
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
count = 0
for row in range(0, len(grid)):
for col in range(0, len(grid[0])):
if grid[row][col] == '1':
self.exploreIsland(grid, row, col)
count += 1
return count
def exploreIsland(self, grid, row, col):
grid[row][col] = '0'
if (row > 0 and grid[row - 1][col] == '1'):
self.exploreIsland(grid, row - 1, col)
if (col > 0 and grid[row][col - 1] == '1'):
self.exploreIsland(grid, row, col - 1)
if (row < len(grid) - 1 and grid[row + 1][col] == '1'):
self.exploreIsland(grid, row + 1, col)
if (col < len(grid[0]) - 1 and grid[row][col + 1] == '1'):
self.exploreIsland(grid, row, col + 1)
2018/10
func explore(grid *[][]byte, row int, col int) {
(*grid)[row][col] = '0'
if row > 0 && (*grid)[row-1][col] == '1' {
explore(grid, row-1, col)
}
if row < len((*grid))-1 && (*grid)[row+1][col] == '1' {
explore(grid, row+1, col)
}
if col > 0 && (*grid)[row][col-1] == '1' {
explore(grid, row, col-1)
}
if col < len((*grid)[0])-1 && (*grid)[row][col+1] == '1' {
explore(grid, row, col+1)
}
}
func numIslands(grid [][]byte) int {
count := 0
for i, row := range grid {
for j, val := range row {
if val == '1' {
explore(&grid, i, j)
count++
}
}
}
return count
}
后续 Follow Up
Q:如何找湖的数量呢?湖的定义是某个0,其上下左右都是同一个岛屿的陆地。
A:我们可以先用Number of island的方法,把每个岛标记成不同的ID,然后过一遍整个地图的每个点,如果是0的话,就DFS看这块连通水域是否被同一块岛屿包围,如果出现了不同数字的陆地,则不是湖。
public class NumberOfLakes {
public static void main(String[] args){
NumberOfLakes nof = new NumberOfLakes();
int[][] world = {{1,1,1,0,1},{1,0,0,1,0},{1,1,1,1,1,},{0,1,1,0,1},{0,1,1,1,1}};
System.out.println(nof.numberOfLakes(world));
}
public int numberOfLakes(int[][] world){
int islandId = 2;
for(int i = 0; i < world.length; i++){
for(int j = 0; j < world[0].length; j++){
if(world[i][j] == 1){
findIsland(world, i, j, islandId);
islandId++;
}
}
}
int lake = 0;
for(int i = 0; i < world.length; i++){
for(int j = 0; j < world[0].length; j++){
if(world[i][j] == 0){
// 找到当前水域的邻接陆地编号
int currId = 0;
if(i > 0) currId = (world[i - 1][j] != 0 ? world[i - 1][j] : currId);
if(j > 0) currId = (world[i][j - 1] != 0 ? world[i][j - 1] : currId);
if(i < world.length - 1) currId = (world[i + 1][j] != 0 ? world[i + 1][j] : currId);
if(j < world[0].length - 1) currId = (world[i][j + 1] != 0 ? world[i][j + 1] : currId);
// 如果该点是湖,加1
if(findLake(world, i, j, currId)){
lake++;
}
}
}
}
return lake;
}
private boolean findLake(int[][] world, int i, int j, int id){
// 将当前水域标记成周边岛屿的数字
world[i][j] = id;
// 找上下左右是否是同一块岛屿的陆地,如果是水域则继续DFS,如果当前水域是边界也说明不是湖
boolean up = i != 0
&& (world[i - 1][j] == id
|| (world[i - 1][j] == 0 && findLake(world, i - 1, j, id)));
boolean down = i != world.length - 1
&& (world[i + 1][j] == id
|| (world[i + 1][j] == 0 && findLake(world, i + 1, j, id)));
boolean left = j != 0
&& (world[i][j - 1] == id
|| (world[i][j - 1] == 0 && findLake(world, i, j - 1, id)));
boolean right = j != world[0].length - 1
&& (world[i][j + 1] == id
|| (world[i][j + 1] == 0 && findLake(world, i, j + 1, id)));
return up && down && right && left;
}
private void findIsland(int[][] world, int i, int j, int id){
world[i][j] = id;
if(i > 0 && world[i - 1][j] == 1) findIsland(world, i - 1, j, id);
if(j > 0 && world[i][j - 1] == 1) findIsland(world, i, j - 1, id);
if(i < world.length - 1 && world[i + 1][j] == 1) findIsland(world, i + 1, j, id);
if(j < world[0].length - 1 && world[i][j + 1] == 1) findIsland(world, i, j + 1, id);
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。