class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int ncount = 0;
for(int n = 0; n < grid.size(); n ++)
{
for(int m = 0; m < grid[n].size(); m++)
{
if(grid[n][m] == 0)
{
continue;
}
else
{
// top
if((n > 0 && (grid[n-1][m] == 0)) || n == 0)
{
ncount ++;
}
// upper
if(((n < grid.size()-1) && (grid[n+1][m] == 0)) || (n == grid.size() - 1))
{
ncount ++;
}
// left
if((m > 0 && (grid[n][m-1] == 0)) || m == 0)
{
ncount ++;
}
// right
if(((m < grid[m].size()-1) && (grid[n][m+1] == 0)) || (m == grid[n].size() - 1))
{
ncount ++;
}
}
}
}
return ncount;
}
};
相关文章
- 03-09463.Island Perimeter
- 03-09LeetCode 463 Island Perimeter 解题报告
- 03-09第一章第八题(圆的面积和周长)(Area and perimeter of a circle)
- 03-09976. Largest Perimeter Triangle(Leetcode每日一题-2020.11.29)
- 03-09LeetCode.976-周长最大的三角形(Largest Perimeter Triangle)
- 03-09976. Largest Perimeter Triangle--Easy
- 03-09463. Island Perimeter*
- 03-09【LeetCode】976. Largest Perimeter Triangle 解题报告(Python)
- 03-09leetcode 463. Island Perimeter
- 03-09洛谷 P3072 [USACO13FEB]Perimeter S(dfs)