Description

Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. If two 1 is adjacent, we consider them in the same island. We only consider up/down/left/right adjacent.

Find the number of islands.

Example

Given graph:

[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]

return 3.

解题:算岛屿的个数。因为连接在一起的陆地都算是岛屿,所以当确定有一一个格子是陆地时,它周围的格子即使是陆地,也不能重复计算,而应该当成海洋(对应于下面代码中的dfs函数)。代码如下:

public class Solution {
/**
* @param grid: a boolean 2D matrix
* @return: an integer
*/
public int numIslands(boolean[][] grid) {
// write your code here
int res = 0;
int m = grid.length;
if(m == 0)
return 0;
int n = grid[0].length;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(grid[i][j] == true){
res++;
dfs(grid, i, j);
}
}
}
return res;
}
public void dfs(boolean[][]grid, int i, int j){
if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length){
return;
}
if(grid[i][j] == true){
grid[i][j] = false;
dfs(grid, i-1, j);
dfs(grid, i+1, j);
dfs(grid, i, j-1);
dfs(grid, i, j+1);
}
}
}

433. Number of Islands【LintCode java】的更多相关文章

  1. 408. Add Binary【LintCode java】

    Description Given two binary strings, return their sum (also a binary string). Example a = 11 b = 1 ...

  2. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  3. 376. Binary Tree Path Sum【LintCode java】

    Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...

  4. 373. Partition Array by Odd and Even【LintCode java】

    Description Partition an integers array into odd number first and even number second. Example Given  ...

  5. 372. Delete Node in a Linked List【LintCode java】

    Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...

  6. 254. Drop Eggs【LintCode java】

    Description There is a building of n floors. If an egg drops from the k th floor or above, it will b ...

  7. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  8. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  9. 423. Valid Parentheses【LintCode java】

    Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...

随机推荐

  1. EF Core 2.1 支持数据库一对一关系

    在使用EF Core和设计数据库的时候,通常一对多.多对多关系使用得比较多,但是一对一关系使用得就比较少了.最近我发现实际上EF Core很好地支持了数据库的一对一关系. 数据库 我们先来看看SQL ...

  2. iOS双滑块选择器

    iOS双滑块选择器 <SDRangeSliderView> https://github.com/qddnovo/SDRangeSliderView 实现了通用性和便利性 今天是个好日子

  3. kafka搭建到配置borker集群(项目开发-区块链)

    (以下分享了搭建kafka需要使用到的命令,差不多齐了,这里没有提到kafka-manager ,同学可以在网上自行查找) 最近公司的项目比较紧,先说下使用kafka的用处: 要替代原来 撮合引擎发数 ...

  4. Python 正则:前后界定和前后非界定

    在用正则去匹配识别手机号.QQ.微信号的时候,往往由于输入的文本可能非常的另类,比如,没有标点和隔断,这时`^`和`$`就用不上了. 不用其实也可以识别的,但是有个问题,手机后是11位数字,QQ是5~ ...

  5. FROM_UNIXTIME/CONCAT

    将mysql查询结果中时间戳转化为时间格式 FROM_UNIXTIME( c.createtime, '%Y-%m-%d %H:%i:%S' ) 2个字段合并查询 CONCAT(d.`name`, ' ...

  6. TF-IDF介绍

    TF-IDF是什么 TF-IDF是一种统计方法,用以评估一个词对于一篇文章或语料库中一篇文章的重要性.字词的重要性随着它在文件中出现的次数成正比增加,但同时会随着它在语料库中出现的频率成反比下降. T ...

  7. 调试日志——基于stm32的智能声光报警器(一)

    今天新画的PCB板子到了,到了手中焊好电源部分测试,没有问题. 测试了下载程序,没有问题.时钟电路供电电路正常. 但是在程序运行的时候发现了问题,程序下载进去了却不运行. 这时候就要从原理图来找问题了 ...

  8. 嵌入式nand flash详解

    一.s3c2440启动后会将nand flash的前4K程序复制到内部的sram中,这个过程是硬件自动完成的,但是如果我们的程序远远大于4K,这个时候就需要将程序从flash拷贝到内存中来运行了. 二 ...

  9. kaggle之员工离职分析

    本文探讨的是kaggle中的一个案例-员工离职分析,从数据集中分析员工的离职原因,并发现其中的问题.数据主要包括影响员工离职的各种因素(工资.绩效.工作满意度.参加项目数.工作时长.是否升职.等)以及 ...

  10. python中Excel表操作

    python中关于excel表个的操作 使用 python中的xlwt和xlrd模块进行操作 # 2003之前:Excel:xls# 2003之后:Excel:xlsx# xlrd:读取的模块:xls ...