463. Island Perimeter
来源:互联网 发布:linux查看ftp用户权限 编辑:程序博客网 时间:2024/06/18 18:15
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.
Example:
[[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]]Answer: 16Explanation: The perimeter is the 16 yellow stripes in the image below:方法1:我的解法,对于每一个岛屿方格,判断其周围的水的数目,特殊情况是行或列为零的情况和只有一行或一列的情况。
class Solution {public: int islandPerimeter(vector<vector<int>>& grid) { int row=grid.size(); int col=grid[0].size(); int sum=0; for(int i=0;i<row;i++) for(int j=0;j<col;j++) { if(grid[i][j]==1) sum+=dodo(grid,i,j,row,col); } return sum; } int dodo(vector<vector<int>>&grid,int i,int j,const int row,const int col) { int n=0; if(i==0||i==row-1) n++; if(row-1==0) n++; if(j==0||j==col-1) n++; if(col-1==0) n++; if(i>=1&&grid[i-1][j]==0) n++; if(j>=1&&grid[i][j-1]==0) n++; if(i<row-1&&grid[i+1][j]==0) n++; if(j<col-1&&grid[i][j+1]==0) n++; return n; }};方法2:每个方格周长为4,如果有相邻,则减1.
0 0
- 463. Island Perimeter
- 463. Island Perimeter
- leetcode 463. Island Perimeter
- leetcode 463. Island Perimeter
- 463. Island Perimeter
- 463. Island Perimeter
- LeetCode 463. Island Perimeter
- 463. Island Perimeter
- Tutorial: 463. Island Perimeter
- 463. Island Perimeter
- 【LeetCode】 463. Island Perimeter
- [leetcode]463. Island Perimeter
- Leetcode-463. Island Perimeter
- 463. Island Perimeter
- Leetcode 463. Island Perimeter
- 463. Island Perimeter
- 463. Island Perimeter*
- 【Leetcode】463. Island Perimeter
- 抽象类的特点总结
- ISP基本框架及算法介绍
- 设计模式之观察者模式
- Adaboost 算法的原理与推导
- SpringBoot非官方教程 | 第七篇:springboot开启声明式事务
- 463. Island Perimeter
- C++知识点:数组末端下一位置
- join查询要点整合
- Controller的使用
- 大多数的愤怒源于自己的无知——《Excel受保护视图》
- 463. Island Perimeter
- 图的遍历
- SpringBoot非官方教程 | 第八篇:springboot整合mongodb
- Shiro框架学习-身份验证