Leetcode——463. Island Perimeter
来源:互联网 发布:dijkstra算法 堆优化 编辑:程序博客网 时间:2024/06/04 23:29
题目
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: 16
Explanation: The perimeter is the 16 yellow stripes in the image below:
解答
题目让求周长(perimeter)
两种方案:
1。寻找规律,边的个数等于4*island-2*neightbour
注意,这里的neighbour是指右邻居(右边有island)和下邻居(下边有island)。每一个邻居相当于抵消2条边。
下面的理解很直观
+--+ +--+ +--+--+| | + | | -> | |+--+ +--+ +--+--+
2。 DFS
代码
class Solution {//use the pattern,the result is "islands * 4 - neighbours * 2",which neighbour refers to the right and down neighbour of the island.public: int islandPerimeter(vector<vector<int>>& grid) { if(grid.size()==0) return 0; int island=0,neighbour=0; for(int i=0;i<grid.size();i++) { for(int j=0;j<grid[0].size();j++) { if(grid[i][j]==1) { island++; if(i<grid.size()-1&&grid[i+1][j]==1) neighbour++; if(j<grid[0].size()-1&&grid[i][j+1]==1) neighbour++; } } } /*for(int i=0;i<grid.size()-1;i++)//This is not right, because either i or j can reach the end. { for(int j=0;j<grid[0].size()-1;j++) { if(grid[i][j]==1&&grid[i+1][j]==1) neighbour++; if(grid[i][j]==1&&grid[i][j+1]==1) neighbour++; } }*/ return 4*island-neighbour*2; }};
class Solution1 {//use the pattern,the result is "islands * 4 - neighbours * 2",which neighbour refers to the right and down neighbour of the island.public: int islandPerimeter(vector<vector<int>>& grid) { //grid passed in should be &(cite) if(grid.size()==0) return 0; for(int i=0;i<grid.size();i++) { for(int j=0;j<grid[0].size();j++) { if(grid[i][j]==1) return helper(grid,i,j); } } return 0; }private: int helper(vector<vector<int>> &grid,int i,int j)// { int count=0; grid[i][j]=-1; //set the value of grid[i][j] -1,in order to avoiding repeating visit if(i-1<0||grid[i-1][j]==0) count++; else if(grid[i-1][j]==1) count+=helper(grid,i-1,j); if(i+1>=grid.size()||grid[i+1][j]==0) count++; else if(grid[i+1][j]==1) count+=helper(grid,i+1,j); if(j-1<0||grid[i][j-1]==0) count++; else if(grid[i][j-1]==1) count+=helper(grid,i,j-1); if(j+1>=grid[0].size()||grid[i][j+1]==0) count++; else if(grid[i][j+1]==1) count+=helper(grid,i,j+1); return count; }};
- Leetcode——463. Island Perimeter
- leetcode 463. Island Perimeter
- leetcode 463. Island Perimeter
- LeetCode 463. Island Perimeter
- 【LeetCode】 463. Island Perimeter
- [leetcode]463. Island Perimeter
- Leetcode-463. Island Perimeter
- Leetcode 463. Island Perimeter
- 【Leetcode】463. Island Perimeter
- LeetCode 463. Island Perimeter
- LeetCode--463. Island Perimeter
- 463. Island Perimeter [LeetCode]
- leetcode 463. Island Perimeter
- leetcode 463. Island Perimeter
- [LeetCode]463. Island Perimeter
- LeetCode | 463. Island Perimeter
- 【LeetCode】463. Island Perimeter
- 【Leetcode】463. Island Perimeter
- Day 01 配置方法
- Andorid AIDL与跨进程通信
- php5.5+apache(X64)环境快速搭建
- Java I/O学习(附实例和详解)
- 设计模式--UML类图关系
- Leetcode——463. Island Perimeter
- 文章标题 诡异的楼梯
- 浅谈hibernate save和saveOrUpdate区别
- 【Java】Java中对象的深复制(深克隆)和浅复制(浅克隆)
- java 如何在pdf中生成表格
- 【mysql】一个因为断电导致无法启动mysql
- Mybatis+Oracle实现按条件分页查询
- maven(一)安装配置
- [sqli-labs]下载与部署