题目如下:

解题思路:这个题目可以进行拆分成几个子问题。第一,求出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的更多相关文章

  1. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  2. 【LeetCode】830. Positions of Large Groups 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】817. Linked List Components 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  6. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  7. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  8. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. MYSQL5.7源码包编译安装

    Centos下用cmake编译安装MySQL 5.7安装依赖包yum -y install gcc gcc-c++ ncurses ncurses-devel cmake下载相应源码包cd /usr/ ...

  2. Selenium学习之==>WebDriverApi接口详解

    浏览器操作 driver.back() # 后退 driver.forward() # 前进 driver.refresh() # 刷新 窗口操作 driver.get_window_size() # ...

  3. DOM4J解析文件

    转发一篇好文 DOM4J解析文件 Dom4j和Xpath

  4. vue--综合组件间的通信

    二.综合组件之间的通信 实现一个ToDoList. ①完成所有的组件的创建和使用 ②add点击add按钮时候,将用户输入的内容(todoinput),显示在(todolist) 核心代码:兄弟组件间通 ...

  5. 【BASIS系列】SAP Basis系统管理中重置用户缓冲哪些需要注意

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[BASIS系列]SAP Basis系统管理中重 ...

  6. 【HBase】三、HBase和RDBMS的比较

      HBase作为一种NoSQL的数据库,和传统的类似于mysql这样的关系型数据库是有很大区别的,本文来对他们做一个对比分析,以便更加深入的了解HBase.   主要区别体现在以下六个方面:   1 ...

  7. swagger 访问格式

    boot工程格式如下 http://10.166.10.169:8085/swagger-ui.html 非boot工程加个自己项目名 http://10.166.10.169:8085/xxx/sw ...

  8. CentOS7 内核模块管理

    1.查看所有模块:lsmod 2.查看指定模块的详细信息:modinfo 模块名 3.动态加载模块:modprobe 模块名 4.动态卸载模块:modprobe -r 模块名 5.开机自动加载模块:假 ...

  9. QQ管理

    ##用例1:查询数据 #01.查询QQ号码为54789625的所有好友信息,包括QQ号码,昵称,年龄 # # SELECT `relation`.RelationQQID AS QQ号码,`basei ...

  10. C++基础-多态

    本文为 C++ 学习笔记,参考<Sams Teach Yourself C++ in One Hour a Day>第 8 版.<C++ Primer>第 5 版.<代码 ...