classSolution { constexprstaticint dx[4] = {0, 1, 0, -1}; constexprstaticint dy[4] = {1, 0, -1, 0}; public: intislandPerimeter(vector<vector<int>> &grid){ int n = grid.size(), m = grid[0].size(); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j]) { int cnt = 0; for (int k = 0; k < 4; ++k) { int tx = i + dx[k]; int ty = j + dy[k]; if (tx < 0 || tx >= n || ty < 0 || ty >= m || !grid[tx][ty]) { cnt += 1; } } ans += cnt; } } } return ans; } };
复杂度分析
时间复杂度:$O(nm)$,其中 n 为网格的高度,$m$ 为网格的宽度。我们需要遍历每个格子,每个格子要看其周围 $4$ 个格子是否为岛屿,因此总时间复杂度为 $O(4nm)=O(nm)$。
classSolution { constexprstaticint dx[4] = {0, 1, 0, -1}; constexprstaticint dy[4] = {1, 0, -1, 0}; public: intdfs(int x, int y, vector<vector<int>> &grid, int n, int m){ if (x < 0 || x >= n || y < 0 || y >= m || grid[x][y] == 0) { return1; } if (grid[x][y] == 2) { return0; } grid[x][y] = 2; int res = 0; for (int i = 0; i < 4; ++i) { int tx = x + dx[i]; int ty = y + dy[i]; res += dfs(tx, ty, grid, n, m); } return res; } intislandPerimeter(vector<vector<int>> &grid){ int n = grid.size(), m = grid[0].size(); int ans = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (grid[i][j] == 1) { ans += dfs(i, j, grid, n, m); } } } return ans; } };