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: 16
Explanation: The perimeter is the 16 yellow stripes in the image below: 题目的意思是,给定某个二维数组,里面全是1和0
1 代表陆地 0代表海洋。相邻的1就是连着的陆地。
把每块陆地想成正方形,边长是1。
现在给定某个二维数组grid,我们来算出相连陆地的总周长。 思想就是遍历整个二位数组,对于每一个数,如果他是陆地 就看他的四周,有一个方向不是陆地 周长就加1 我的代码:
class Solution(object):
def islandPerimeter(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
res = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
if i == 0 or grid[i-1][j] == 0:
res +=1
if i==len(grid)-1 or grid[i+1][j]==0:
res +=1
if j==0 or grid[i][j-1] ==0:
res +=1
if j==len(grid[0])-1 or grid[i][j+1]==0:
res += 1
return res if __name__ == '__main__':
s = Solution()
res = s.islandPerimeter( [
[0,1,0,0],
[1,1,1,0],
[0,1,0,0],
[1,1,0,0]
])
print(res)
leetcode算法:Island Perimeter的更多相关文章
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- LeetCode 463. Island Perimeter (岛的周长)
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- [LeetCode] 463. Island Perimeter 岛的周长
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- LeetCode 463 Island Perimeter 解题报告
题目要求 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 rep ...
- LeetCode 463. Island Perimeter岛屿的周长 (C++)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- LeetCode - 463. Island Perimeter - O(MN)- (C++) - 解题报告
原题 原题链接 You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 ...
- LeetCode: 463 Island Perimeter(easy)
题目: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 repr ...
- 3. leetcode 463 Island Perimeter
思路:设原始周长为4*节点数,每当出现一次相邻的情况,原始周长会减2.
- LeetCode算法题-Island Perimeter(Java实现)
这是悦乐书的第238次更新,第251篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第105题(顺位题号是463).您将获得一个二维整数网格形式的地图,其中1代表土地,0代 ...
- 463. Island Perimeter - LeetCode
Question 463. Island Perimeter Solution 题目大意:给出一个二维数组1表示陆地0表示海,求陆地的周长 思路: 重新构造一张地图grid2即一个二维数组,比原数组大 ...
随机推荐
- WordPress修改标签云大小及颜色
修改WordPress标签字体大小: 在cpanel面板中依次打开「wp-includes」→「category-template.php」,找到wp_tag_cloud, 1 2 3 4 5 6 7 ...
- 第一周Python讲课内容--日记
1.python的发展史,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年...... 2.第一个helloword程序的开始 3.变量的含义,赋值传参数的作 ...
- python 备份压缩传输
# -*- coding:utf-8 -*-__author__ = 'colin' #!/usr/bin/env python#-*- coding:utf-8 -*-import os,comma ...
- load vs. initialize
这篇文章来对比一下NSObject类的两个方法,+load与+initialize. + (void)load; Invoked whenever a class or category is add ...
- FastJson简单使用
首先建立两个实体类,Student.java 和 Teacher.java public class Student { private int id; private String name; pr ...
- 实现Java线程安全
一个类如果想要满足线程安全的条件: 每个线程都能正常的执行原子操作,保证得到正确的结果 这个类的对象可以同时被多个线程安全的访问 在每个线程的原子操作都完成后,对象处于合理的状态 一般情况下不可变类总 ...
- JQ 判断 浏览器打开的设备类型
<script> $(document).ready(function(){ var ua = navigator.userAgent; var ipad = ua.match(/(iPa ...
- ReactNative环境配置的坑
我用的是windows开发android,mac的可以绕道了. 1.android studio及Android SDK的安装 现在需要的Android版本及对应的tool 2.真机运行要配置对and ...
- WPF 16进制byte输入框
在WPF中,针对byte类型的输入控件可以选用 XCEED 的免费库中的 Xceed.Wpf.Toolkit.ByteUpDown(可从nuget获取). 若要使该控件在界面上以16进制显示byte, ...
- [iOS]详解调整UIButton的title和image的位置
UIButton的默认布局是:title在右,image在左; 很多时候我们需要的是title在左边,或者title在下面,这时就需要调整UIButton的TitleLabel和ImageView的位 ...