2017/11/13 Leetcode 日记

2017/11/13 Leetcode 日记

463. Island Perimeter

You are given a map in form of a two-dimensional integer grid where 1 represents land and 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" (water inside that 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.

(求给出格子的周长,注意有可能多个联通块)

2017/11/13 Leetcode 日记

class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int row = grid.size(), col = grid[].size();
int count = , edge = ;
for(int i = ; i < row; i++){
for(int j = ; j < col; j++){
if(grid[i][j]){
count ++;
if(i && grid[i-][j]) edge ++;
if(j && grid[i][j-]) edge ++;
}
}
}
return * count - * edge;
}
};

c++ 非递归实现

上一篇:[HMLY]7.iOS MVVM+RAC 从框架到实战


下一篇:2017/11/9 Leetcode 日记