問題:code
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.orm
Example 1:遞歸
11110 11010 11000 00000
Answer: 1io
Example 2:form
11000 11000 00100 00011
Answer: 3class
解決:grid
① 求島嶼數量的題的本質是求矩陣中連續區域的個數,採用DFS,基本思想是合併相鄰的lands,合併經過遞歸完成。時間複雜度爲O(m*n)。map
class Solution { //6ms
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
int m = grid.length;
int n = grid[0].length;
int count = 0;
for (int i = 0;i < m;i ++){
for (int j = 0;j < n;j ++){
if (grid[i][j] == '1'){
count ++;
merge(grid,i,j);
}
}
}
return count;
}
public void merge(char[][] grid,int i,int j){
int m = grid.length;
int n = grid[0].length;
if(i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != '1') return;
grid[i][j] = '*';
merge(grid,i - 1,j);
merge(grid,i + 1,j);
merge(grid,i,j - 1);
merge(grid,i,j + 1);
}
}call