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
<Solution>典型的 DFS 題目,想法如下
- 找到入口點後,把所有連在一起的 '1' 都找出來,並換成其他字元
- 每找到一次入口點,答案就加 1
C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
private: | |
int row; | |
int col; | |
public: | |
int numIslands(vector<vector<char>>& grid) { | |
int ans = 0; | |
if(!grid.empty()) { | |
row = grid.size(); | |
col = grid[0].size(); | |
for(int r = 0; r < row; r++) { | |
for(int c = 0; c < col; c++) { | |
if(grid[r][c] == '1') { | |
dfs(r, c, grid); | |
++ans; | |
} | |
} | |
} | |
} | |
return ans; | |
} | |
void dfs(const int &curR, const int &curC, vector<vector<char>> &grid) { | |
grid[curR][curC] = 'x'; | |
for(int r = -1; r < 2; r += 2) { | |
int nextR = curR + r; | |
if((nextR >= 0 && nextR < row) && | |
grid[nextR][curC] == '1') { | |
dfs(nextR, curC, grid); | |
} | |
} | |
for(int c = -1; c < 2; c += 2) { | |
int nextC = curC + c; | |
if((nextC >= 0 && nextC < col) && | |
grid[curR][nextC] == '1') { | |
dfs(curR, nextC, grid); | |
} | |
} | |
} | |
}; |
Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int numIslands(char[][] grid) { | |
if(grid.length == 0 || grid[0].length == 0) { | |
return 0; | |
} | |
final int row = grid.length; | |
final int col = grid[0].length; | |
int ans = 0; | |
for(int r = 0; r < row; r++) { | |
for(int c = 0; c < col; c++) { | |
if(grid[r][c] == '1') { | |
dfs(grid, r, c); | |
++ans; | |
} | |
} | |
} | |
return ans; | |
} | |
private void dfs(char[][] grid, int row, int col) { | |
if(row < 0 || row >= grid.length) { | |
return; | |
} | |
else if(col < 0 || col >= grid[0].length) { | |
return; | |
} | |
else if(grid[row][col] == 'x' || grid[row][col] == '0') { | |
return; | |
} | |
grid[row][col] = 'x'; | |
for(int r = -1; r < 2; r += 2) { | |
dfs(grid, row + r, col); | |
} | |
for(int c = -1; c < 2; c += 2) { | |
dfs(grid, row, col + c); | |
} | |
} | |
} |
kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun numIslands(grid: Array<CharArray>): Int { | |
var ans = 0 | |
for(r in grid.indices) { | |
for(c in grid[0].indices) { | |
if(grid[r][c] == '1') { | |
++ans | |
dfs(grid, r, c) | |
} | |
} | |
} | |
return ans | |
} | |
fun dfs(grid: Array<CharArray>, row: Int, col: Int) { | |
when { | |
row < 0 || row > grid.lastIndex -> return | |
col < 0 || col > grid[0].lastIndex -> return | |
grid[row][col] == '0' || grid[row][col] == '*' -> return | |
else -> { | |
grid[row][col] = '*' | |
for(r in -1..1 step 2) { | |
dfs(grid, row+r, col) | |
} | |
for(c in -1..1 step 2) { | |
dfs(grid, row, col+c) | |
} | |
} | |
} | |
} | |
} |
沒有留言:
張貼留言