LintCode 433. 岛屿的个数(Number of Islands)
LintCode 433. 岛屿的个数(Number of Islands)
代码:
class Solution:
"""
@param grid: a boolean 2D matrix
@return: an integer
"""
def numIslands(self, grid):
# write your code here
n_Islands=0
n_rows=len(grid)
if n_rows:
n_cols=len(grid[0])
for i in range(n_rows):
for j in range(n_cols):
if grid[i][j]==-1 or grid[i][j]==0:
continue
else:
self.Islands(grid,i,j,n_rows,n_cols)
n_Islands+=1
return n_Islands
def Islands(self,grid,i,j,n_rows,n_cols):
if i<0 or j<0 or i>n_rows-1 or j>n_cols-1:
return
elif grid[i][j]==-1 or grid[i][j]==0:
return
elif grid[i][j]==1:
grid[i][j]=-1
self.Islands(grid,i-1,j,n_rows,n_cols)
self.Islands(grid,i+1,j,n_rows,n_cols)
self.Islands(grid,i,j+1,n_rows,n_cols)
self.Islands(grid,i,j-1,n_rows,n_cols)
return
LintCode 433. 岛屿的个数(Number of Islands)的更多相关文章
- [Swift]LeetCode200.岛屿的个数 | Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)
Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...
- lintcode 443.岛屿的个数
在v2ex上看到有人提到了这个,感觉挺简单的,没忍住还是试一下.... 基本的染色法. AC代码: public class Solution { /** * @param grid a boolea ...
- [LintCode] Number of Islands(岛屿个数)
描述 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], [0, 1, ...
- lintcode:Number of Islands 岛屿的个数
题目: 岛屿的个数 给一个01矩阵,求不同的岛屿的个数. 0代表海,1代表岛,如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右为相邻. 样例 在矩阵: [ [1, 1, 0, 0, 0], ...
- [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] Number of Distinct Islands 不同岛屿的个数
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [Swift]LeetCode305. 岛屿的个数 II $ Number of Islands II
A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
随机推荐
- my_mysql
###一键偷懒YUM安装MySQbL### 1.安装mysql数据库 #yum install -y mariadb-server mariadb 2.登录mysql数据库常用选项 -h:指定服务端 ...
- Oracle基础之保留字和关键字
Oracle基础之保留字和关键字 在Oracle之中,有分为保留字和关键字,所谓关键字就是Oracle中有实际意义的,而保留字(比如DESC.ORDER等等)是Oracle中不能随便使用的,比如不能随 ...
- Markdown 复杂公式&常用符号
公式格式 行内公式 行内公式(不会换行)使用 $ 作为起止符,例如:$a + b = c$, 效果为:\(a + b = c\) 块级公式 块级公式(单独一行)使用 $$ 作为起止符,例如:$$a + ...
- nginx 负载均衡及反向代理
Nginx简介 Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.由俄罗斯的程序设计师开发,官方测试nginx能够支支撑5万并发链接,并且cpu.内存 ...
- 开发STM32MP1,你需要一块好开发板
STM32MP1系列的出现吸引了很多STM32的新老用户的关注,但是很多的人都会担心一个问题:以前是基于Cortex M系列MCU惊醒开发,对于cortex-A架构的处理器以及Linux系统都不熟悉. ...
- 《深入理解 Java 虚拟机》读书笔记:虚拟机性能监控与故障处理工具
正文 一.JDK 的命令行工具 JDK 的 bin 目录下提供了一些用于监视虚拟机和故障处理的命令行工具. 名称 主要作用 jps JVM Process Status Tool,显示正在运行的虚拟机 ...
- js中如何将伪数组转换成数组
伪数组:不能调用数组的方法, 1.对象是按索引方式存储数据的 2.它具备length属性 {0:'a',1:'b',length:2} //es5伪数组转换成数组 let args = [].slic ...
- 发布到远程存储库时遇到错误: Git failed with a fatal error.
正在推送 master发布到远程存储库时遇到错误: Git failed with a fatal error.Authentication failed for 'http://1212121xxx ...
- MySQL查询基础
MySQL查询 DQL(Data Query Language ) 1.排序查询 # 语法: select 字段 from 表名 order by 字段1 [降序/升序],字段2 [降序/升序],.. ...
- iOS的项目目录结构
一.一般面试官都会问这样的一个问题,你怎样划分你项目的目录结构,就能测试出这个人是否有经验? 目前,比较常规的两种结构: 1.主目录按照业务分类,内目录按照模块分类(主目录按照MVC架构分类,内部根据 ...