463 Island Perimeter 岛屿的周长
详见:https://leetcode.com/problems/island-perimeter/description/
C++:
class Solution { public: int islandPerimeter(vector<vector<int>>& grid) { if (grid.empty() || grid[0].empty()) { return 0; } int m = grid.size(), n = grid[0].size(), res = 0; for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (grid[i][j] == 0) { continue; } if (j == 0 || grid[i][j - 1] == 0) { ++res; } if (i == 0 || grid[i - 1][j] == 0) { ++res; } if (j == n - 1 || grid[i][j + 1] == 0) { ++res; } if (i == m - 1 || grid[i + 1][j] == 0) { ++res; } } } return res; } };
参考:https://www.cnblogs.com/grandyang/p/6096138.html