200.Number of Islands
Given an m x n 2D binary grid grid which represents a map of '1’s (land) and '0’s (water), return 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:
Input: grid = [
[“1”,“1”,“1”,“1”,“0”],
[“1”,“1”,“0”,“1”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“0”,“0”,“0”]
]
Output: 1
Example 2:
Input: grid = [
[“1”,“1”,“0”,“0”,“0”],
[“1”,“1”,“0”,“0”,“0”],
[“0”,“0”,“1”,“0”,“0”],
[“0”,“0”,“0”,“1”,“1”]
]
Output: 3
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] is ‘0’ or ‘1’.
class Solution {
public int numIslands(char[][] grid) {
if (grid == null)
return 0;
int rows = grid.length;
int cols = grid[0].length;
int ans = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
ans += dfs(grid, i, j);
}
}
return ans;
}
public int dfs(char[][] grid, int r, int c) {
// 若出界则直接退出
if (r == grid.length || c == grid[0].length
|| r < 0 || c < 0)
return 0;
// 若是水也直接退出
if (grid[r][c] == '0')
return 0;
// 给访问过的小岛做标记
// 若无此标记则会一直兜圈
grid[r][c] = '0';
dfs(grid, r-1, c);
dfs(grid, r+1, c);
dfs(grid, r, c-1);
dfs(grid, r, c+1);
// 递归的访问一个小岛的上下左右四个方向后
// 返回值1, 表示一个大岛屿
return 1;
}
}
463. Island Perimeter
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn’t have “lakes”, meaning the water inside isn’t connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.
Example 1:
Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
Explanation: The perimeter is the 16 yellow stripes in the image above.
Example 2:
Input: grid = [[1]]
Output: 4
Example 3:
Input: grid = [[1,0]]
Output: 4
Constraints:
row == grid.length
col == grid[i].length
1 <= row, col <= 100
grid[i][j] is 0 or 1.
由于前几天做过200.Number of Islands这道题,自然而然就想到在dfs时顺便统计边数,怎么个统计法嘞?
一个岛屿的周长其实就是与岛屿方格相邻的水方格的数量
(这里的水方格既包括在grid里的, 当然也包括grid外的)
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int cnt = 0;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
if (grid[i][j] != 0 && grid[i][j] != -1)
cnt = dfs(grid, i, j);
}
}
return cnt;
}
int dfs(vector<vector<int>>& grid, int r, int c) {
if (r<0 || c<0 || r==grid.size() || c==grid[0].size())
return 1;
if (grid[r][c] == 0)
return 1;
if (grid[r][c] == -1)
return 0;
grid[r][c] = -1;
return dfs(grid, r+1, c) +
dfs(grid, r-1, c) +
dfs(grid, r, c+1) +
dfs(grid, r, c-1);
}
};
// 时间复杂度: O(mn)
// 空间复杂度: O(mn)
但用dfs属实是多此一举 -e-
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
int ans = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
if (0==i || grid[i-1][j]==0) ans++; // 上
if (rows-1==i || grid[i+1][j]==0) ans++; // 下
if (0==j || grid[i][j-1]==0) ans++; // 左
if (cols-1==j || grid[i][j+1]==0) ans++; // 右
}
}
}
return ans;
}
};
// 时间复杂度: O(mn)
// 空间复杂度: O(1)
to be continued …