Level:

  Medium

题目描述:

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

Example 1:

Input:
11110
11010
11000
00000 Output: 1

Example 2:

Input:
11000
11000
00100
00011 Output: 3

思路分析:

​  寻找岛屿的数量,如果遇到一个1,四周都是0,那么为一个岛,只要水平或者垂直临近的1都算是1个岛,求一个二维数组中岛的个数。即求数组中不相邻的1的个数,解决思路,遍历数组,碰到一个1,就把周围所有相连的1都标记为非1,这样整个遍历过程中碰到的1的个数就是所求的解。

代码:

public class Solution{
public int numIslands(char [][]grid){
if(grid==null||grid.length==0)
return 0;
int res=0;
for(int i=0;i<grid.length;i++){
for(int j=0;j<grid[0].length;j++){
if(grid[i][j]!='1')
continue;
res++;
dfs(grid,i,j);
}
}
return res;
}
public void dfs(char [][]grid,int i,int j){
if(i<0||i==grid.length||j<0||j==grid[0].length)
return;
if(grid[i][j]=='1'){
grid[i][j]='0';
dfs(grid,i+1,j);
dfs(grid,i,j+1);
dfs(grid,i-1,j);
dfs(grid,i,j-1);
}
}
}

47.Number of Islands(岛的数量)的更多相关文章

  1. LeetCode Number of Islands 岛的数量(DFS,BFS)

    题意:0代表水,1代表陆地,那么被水围起来的就是岛了,给一个01矩阵,问有多少个岛? 思路:DFS还是比较短,实现了一下.如果一个点已经被遍历过了,那就将其置为0就行了,不要去搜0的. class S ...

  2. [LeetCode] 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 ...

  3. [LeetCode] 200. 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 ...

  4. [LintCode] Number of Islands 岛屿的数量

    Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...

  5. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  6. Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands)

    Leetcode之深度优先搜索(DFS)专题-200. 岛屿数量(Number of Islands) 深度优先搜索的解题详细介绍,点击 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计 ...

  7. LeetCode 200:岛屿数量 Number of Islands

    题目: 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. Given ...

  8. [LeetCode] 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 ...

  9. [LeetCode] 305. 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 ...

随机推荐

  1. HTML学习笔记(四)常用标签

    1.超链接 <a href="url">Link text</a> eg:<a href="http://www.w3school.com. ...

  2. QRegExp解析

    正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征.比如 表达式“ab+” 描述的特征是“一个 'a' 和 任意个 'b' ...

  3. layui icon样式1到7

    1: 2: 3: 4: 5: 6: 7:

  4. numpy windows环境下载安装

    由于numpy在多个平台下非常流行,以至于习惯WINDOWS环境下的用户可能找不到下载位置,更多的时候会下载到zip文件,然后需要安装编译(自然通不过) 1.http://www.scipy.org/ ...

  5. MVC3+jquery Uploadify 上传文件

    最近做项目用到了上传图片的功能,以前也写过这类代码,不过都是用传统的file标签,今天整理一个好用的插件Uploadify..都做了一些注释,一看便知. 可以去官网下载最新的:Uploadify下载地 ...

  6. shell学习(2)- sed

    一.替换(s) 详细用法  [address]s/pattern/replacement/flags 修饰flag的标志如下: 命令 说明 n 1到512之间的一个数字,表示文本模式中指定模式第n次出 ...

  7. activity fragment 传值

    Bundle bundle = new Bundle(); bundle.putString("key","value"); fragment.setArgum ...

  8. SpringBoot | Hibernate @Transient 注解

    在默认情况下,持久化类的所有属性会自动映射到数据表的数据列.如果在实际应用中,不想持久保存某些属性,则可以考虑使用@Transient来修饰它们. 如果一个属性并非数据库表的字段映射,就务必将其标示为 ...

  9. IMG 的alt和title的区别(转自 百度空间--路云的世界)

    图片标签img中alt与title的区别 图片标签img中alt与title的区别 可能很多新手在做站内优化的时候,不明白图片标签img中alt与title的区别,今天为大家说一下其中的区别. 大家可 ...

  10. js操作表格

    js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法 表格有几行: var trCnt = table.rows.length ...