problem

​463. Island Perimeter​

solution:

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
if(grid.empty()) return 0;
int m=grid.size(), n=grid[0].size();
int ans = 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) ans++;
if(j==n-1 || grid[i][j+1]==0) ans++;
if(i==0 || grid[i-1][j]==0) ans++;
if(i==m-1 || grid[i+1][j]==0) ans++;
}
}
return ans;
}
};

 

 

参考

1. ​​Leetcode_463. Island Perimeter​​;