给你一个由 '1'
(陆地)和 '0'
(水)组成的的二维网格,请你计算网格中岛屿的数量。
岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外,你可以假设该网格的四条边均被水包围。
示例 1:
输入:grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"], ["1","1","0","0","0"], ["0","0","0","0","0"] ] 输出:1
示例 2:
输入:grid = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] 输出:3
提示:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j]
的值为'0'
或'1'
BFS、DFS、并查集均可。
DFS:
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
def dfs(i, j):
if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]) or grid[i][j] != '1':
return
grid[i][j] = '0'
for x, y in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
dfs(i + x, j + y)
ans = 0
m, n = len(grid), len(grid[0])
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
dfs(i, j)
ans += 1
return ans
并查集:
class Solution:
def numIslands(self, grid: List[List[str]]) -> int:
m, n = len(grid), len(grid[0])
p = list(range(m * n))
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
for i in range(m):
for j in range(n):
if grid[i][j] == '1':
if i < m - 1 and grid[i + 1][j] == '1':
p[find(i * n + j)] = find((i + 1) * n + j)
if j < n - 1 and grid[i][j + 1] == '1':
p[find(i * n + j)] = find(i * n + j + 1)
res = 0
for i in range(m):
for j in range(n):
if grid[i][j] == '1' and i * n + j == find(i * n + j):
res += 1
return res
DFS:
class Solution {
private int[][] dirs = new int[][]{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
public int numIslands(char[][] grid) {
int ans = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
dfs(grid, i, j);
++ans;
}
}
}
return ans;
}
public void dfs(char[][] grid, int i, int j) {
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] != '1') {
return;
}
grid[i][j] = '0';
for (int[] dir : dirs) {
dfs(grid, i + dir[0], j + dir[1]);
}
}
}
并查集:
class Solution {
private int[] p;
public int numIslands(char[][] grid) {
int m = grid.length, n = grid[0].length;
p = new int[m * n];
for (int i = 0; i < p.length; ++i) {
p[i] = i;
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
if (i < m - 1 && grid[i + 1][j] == '1') {
p[find(i * n + j)] = find((i + 1) * n + j);
}
if (j < n - 1 && grid[i][j + 1] == '1') {
p[find(i * n + j)] = find(i * n + j + 1);
}
}
}
}
int res = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1' && i * n + j == find(i * n + j)) {
++res;
}
}
}
return res;
}
private int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
}
function numIslands(grid: string[][]): number {
let m = grid.length,
n = grid[0].length;
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
dfs(grid, i, j);
++ans;
}
}
}
return ans;
}
function dfs(grid: string[][], i: number, j: number) {
let m = grid.length,
n = grid[0].length;
if (i < 0 || i > m - 1 || j < 0 || j > n - 1 || grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
for (let [dx, dy] of [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
]) {
let x = i + dx,
y = j + dy;
dfs(grid, x, y);
}
}
class Solution {
public:
vector<int> p;
int numIslands(vector<vector<char>>& grid) {
int m = grid.size(), n = grid[0].size();
p.resize(m * n);
for (int i = 0; i < p.size(); ++i) p[i] = i;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (grid[i][j] == '1')
{
if (i < m - 1 && grid[i + 1][j] == '1') p[find(i * n + j)] = find((i + 1) * n + j);
if (j < n - 1 && grid[i][j + 1] == '1') p[find(i * n + j)] = find(i * n + j + 1);
}
}
}
int res = 0;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j < n; ++j)
{
if (grid[i][j] == '1' && i * n + j == find(i * n + j)) ++res;
}
}
return res;
}
int find(int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};
并查集:
var p []int
func numIslands(grid [][]byte) int {
m, n := len(grid), len(grid[0])
p = make([]int, m*n)
for i := 0; i < len(p); i++ {
p[i] = i
}
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == '1' {
if i < m-1 && grid[i+1][j] == '1' {
p[find(i*n+j)] = find((i+1)*n + j)
}
if j < n-1 && grid[i][j+1] == '1' {
p[find(i*n+j)] = find(i*n + j + 1)
}
}
}
}
res := 0
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if grid[i][j] == '1' && i*n+j == find(i*n+j) {
res++
}
}
}
return res
}
func find(x int) int {
if p[x] != x {
p[x] = find(p[x])
}
return p[x]
}