【leetcode】827. Making A Large Island
题目如下:

解题思路:这个题目可以进行拆分成几个子问题。第一,求出island的数量,其实就是 200. Number of Islands,这个很简单,DFS或者BFS都能搞定;第二,除了求出island的数量之外,还要求出每个island包括的1的数量,这个也不难,在DFS或者BFS的过程中计数即可;第三,遍历grid中所有的0,判断每个0的上下左右分别连接了几个不同的island,并将连接的所有island的所有1的数量求和,再加上本身的1(0变来的)即是这个0变成1可以得到的large island,最后,求出所有0的最大的large island即可。
代码如下:
class Solution(object):
def largestIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
for i in grid:
i.append('#')
i.insert(0,'#')
grid.append(['#' for i in grid[0]])
grid.insert(0,['#' for i in grid[0]])
visit = []
for i in grid:
visit.append([0 for x in i])
queue = [] l = [0] area = 1
num = 1 for i in xrange(1,len(grid)):
for j in xrange(1,len(grid[i])):
if grid[i][j] == '#' or grid[i][j] == 0:
continue
if visit[i][j] != 0:
continue
queue.append((i,j,area))
visit[i][j] = area
while len(queue) > 0:
x,y,a = queue.pop(0) # a为island的编号,用来记录一个有几个island
if grid[x+1][y] == 1 and visit[x+1][y] == 0:
num += 1
queue.append((x+1,y,a))
visit[x + 1][y] = a
if grid[x-1][y] == 1 and visit[x-1][y] == 0:
num += 1
queue.append((x-1,y,a))
visit[x - 1][y] = a
if grid[x][y+1] == 1 and visit[x][y+1] == 0:
num += 1
queue.append((x,y+1,a))
visit[x][y + 1] = a
if grid[x][y-1] == 1 and visit[x][y-1] == 0:
num += 1
queue.append((x,y-1,a))
visit[x][y - 1] = a
area += 1
l.append(num) #l为每个island的1的数量
num = 1
res = 0
for i in l:
if res < i:
res = i
#print visit,l
for i in xrange(1,len(grid)):
for j in xrange(1,len(grid[i])):
if grid[i][j] == 0:
count = 1
al = []
if grid[i+1][j] == 1:
if visit[i+1][j] not in al:
count += l[visit[i+1][j]]
al.append(visit[i+1][j])
if grid[i-1][j] == 1:
if visit[i-1][j] not in al:
count += l[visit[i-1][j]]
al.append(visit[i-1][j])
if grid[i][j+1] == 1:
if visit[i][j+1] not in al:
count += l[visit[i][j+1]]
al.append(visit[i][j+1])
if grid[i][j-1] == 1:
if visit[i][j-1] not in al:
count += l[visit[i][j-1]]
al.append(visit[i][j-1])
if res < count:
res = count
return res
【leetcode】827. Making A Large Island的更多相关文章
- 【LeetCode】695. Max Area of Island 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- 【LeetCode】830. Positions of Large Groups 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】817. Linked List Components 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_2 连接池介绍
- HTTP学习记录:三、状态码
学习资源主要为:@小坦克HTTP相关博客 常见的HTTP状态码: 200--请求成功: 302--重定向: 304--Not Modified:表示上次的文档已经被缓存了,还可以继续使用: 400-- ...
- HTTP学习记录:二、请求方法
学习资源主要为:@小坦克HTTP相关博客 最基础的是4种,GET.POST.PUT.DELETE对应着对资源的查.改.增.删.最常用的是GET和POST. GET一般用于获取/查询资源信息: POST ...
- 系统分析与设计HW2
简答题 1. 简述瀑布模型.增量模型.螺旋模型(含原型方法)的优缺点. 瀑布模型 优点: 定义了软件开发基本流程与活动. 为项目提供了按阶段划分的检查点. 当前一阶段完成后,只需关注后续阶段. 缺点: ...
- PHP 数组函数 内部指针
current( &$arr ) 每个数组的当前单元,初始值的 数组的第一个单元next ( &$arr ) 返回数组中的下一个单元 , 如果没值则返回falshprev ( & ...
- CSS3——提示工具 图片廓 图像透明 图像拼接技术 媒体类型 属性选择器
提示工具 提示框在鼠标移动到特定的元素上显示 设置提示框的位置 给提示框添加箭头 提示框的淡入效果 提示框美化 图片廓 响应式图片廓 图像透明 创建透明图像——悬停效果 ———鼠标放置后———> ...
- Java——LinkedHashMap源码解析
以下针对JDK 1.8版本中的LinkedHashMap进行分析. 对于HashMap的源码解析,可阅读Java--HashMap源码解析 概述 哈希表和链表基于Map接口的实现,其具有可预测的迭 ...
- 简述Vue的实例属性、实例方法
1.实例属性 组件树访问 $parent -----> 用来访问当前组件实例的父实例: $root -----> 用来访问当前组件树的根实例,如果当前组件没有父实例,则$root表示当前组 ...
- Charles抓包过滤的四种方式
日常测试中,经常要抓包看请求的request,response是不是传的对,返回的字段值对不对,众多的请求中如何找到自己想要的请求,就需要过滤请求,Charles有4种过滤方式,用那一种都可以,看个人 ...
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)
https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...