leetcode算法: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.

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和01 代表陆地 0代表海洋。相邻的1就是连着的陆地。把每块陆地想成正方形,边长是1。现在给定某个二维数组grid,我们来算出相连陆地的总周长。

思想就是遍历整个二位数组,对于每一个数,如果他是陆地 就看他的四周,有一个方向不是陆地 周长就加1

我的代码:
 1 class Solution(object):
 2     def islandPerimeter(self, grid):
 3         """
 4         :type grid: List[List[int]]
 5         :rtype: int
 6         """
 7         res = 0
 8         for i in range(len(grid)):
 9             for j in range(len(grid[0])):
10                 if grid[i][j] == 1:
11                     if i == 0 or grid[i-1][j] == 0:
12                         res +=1
13                     if i==len(grid)-1 or grid[i+1][j]==0:
14                         res +=1
15                     if j==0 or grid[i][j-1] ==0:
16                         res +=1
17                     if j==len(grid[0])-1 or grid[i][j+1]==0:
18                         res += 1
19         return res
20
21
22 if __name__ == ‘__main__‘:
23     s = Solution()
24     res = s.islandPerimeter( [
25                      [0,1,0,0],
26                      [1,1,1,0],
27                      [0,1,0,0],
28                      [1,1,0,0]
29                 ])
30     print(res)
时间: 2024-10-22 07:21:59

leetcode算法:Island Perimeter的相关文章

Leetcode 463. Island Perimeter JAVA语言

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 isla

LeetCode: 463 Island Perimeter(easy)

题目: 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

[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 isla

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

463. Island Perimeter - LeetCode

Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大一圈,即长宽都大2 一个点在原地图坐标是(i,j),那么在重新构造的坐标就是(i+1,j+1) 遍历原地图,如果一是陆地,就遍历这个点的周围是否是海,如果是海线周长就加1 Java实现: public int islandPerimeter(int[][] grid) { int total = 0

Leetcode-463 Island Perimeter

#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 th

LeetCode算法编程(两题)

今天看到酷壳推荐的国外编程LeetCode算法编程网站,上面目前有154道算法题,感觉很有意思,平常工作也比较忙,现在很少有时间来锻炼算法相关的东西,有空的时候静下心来,温习下基础,活跃下自已的思路,也是有必要的.先做了几道,后面会陆续补充其它的题目. 1.题目-PlusOne Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored s

Leetcode 算法题--ReverseWordsInString

翻转字符串,想到什么写什么...我的做法是先trim掉空格,然后从字符串尾部开始扫描,遇到空格则认为一个单词结束,然后copy这个单词.需要注意的地方在于当扫描到最后一个单词的第一个字母时(譬如the sky is blue的t字母),注意单词长度的自增逻辑. 网上还有人的做法是反转整个字符串,然后逐个翻转单词. 1 package edu.hust.sse.Problems; 2 3 //Given s = "the sky is blue", 4 //return "bl

leetcode 算法 之 马拉松算法(Manacher's algorithm)(未完成)

马拉松算法:马拉松算法是用来计算一个字符串中最长的回文字符串(对称字符串,如aba abba). 首先,我们拿到一个字符串S,然后在S中的每个字符之间加#.例如:S="abcb" T="a#b#c#b" 我们T字符串的每一个T[i]向延伸d个字符 使得 T[i-d,i+d]是一个回文字符串.你会立刻发现,d就是以T[i]为中心的最长回文字符串的长度. 我们建立一个P数组,是的P数组的长度等于T的长度,每一个P[i]的值表示对应的T[i]为中心的最大回文字符串的长度.

leetcode算法: Find Bottom Left Tree Value

leetcode算法: Find Bottom Left Tree Value Given a binary tree, find the leftmost value in the last row of the tree. Example 1:Input: 2 / \ 1 3 Output:1Example 2: Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output:7Note: You may assume the tree (i.e., the given ro