【leetcode】463. Island Perimeter【E】
来源:互联网 发布:虚拟机上装linux 编辑:程序博客网 时间:2024/04/29 19:26
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:
Subscribe to see which companies asked this question
算法很简单,其实就是一共多少个1 ,乘以4就是一共有多少条边,然后再看有多少个相邻的边,每条相邻的边,减2,就对了
比如上面这个图,一共7个1 ,共28条边,6条相邻的边,28-12 = 16
class Solution(object): def islandPerimeter(self, grid): res = 0 for i in grid: res += sum(i) g= grid res *= 4 i,j = 0,0 while i < len(g): j = 0 while j < len(g[0]): if j + 1< len(g[0]) and g[i][j] == 1 and g[i][j+1] == 1: res -= 2 j += 1 i += 1 i,j = 0,0 while i < len(g[0]): j = 0 while j < len(g): if j + 1 < len(g) and g[j][i] == 1 and g[j+1][i] == 1: res -= 2 j += 1 i += 1 return res
0 0
- 【leetcode】463. Island Perimeter【E】
- 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
- Lucene开发实例(一般企业搜索平台完全够用全程)
- 【bzoj1602】牧场行走 lca
- spark sql中踩到的一个坑,自定义Udf会执行多次,即使在已经cache table的情况下
- 【bzoj3670】动物园 kmp
- 【bzoj1030】文本生成器 AC自动机+树型dp
- 【leetcode】463. Island Perimeter【E】
- 曹冲养猪 中国剩余定理
- 【HDU1573】X问题 中国剩余定理
- 【bzoj2818】Gcd 欧拉函数
- java自带线程池和队列详细讲解
- git笔记:一篇搞懂git是个什么玩意
- 【bzoj3667】Rabin-Miller算法 素数测试
- UFLDL教程Exercise答案(3.1):PCA in 2D
- JavaEE:Spring-security笔记