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. javascript runat server

    <script runat="server"> protected void SubmitBtn_Click(object sender, EventArgs e) { ...

  2. STL-开篇

    基本概念 STL: Standard Template Library,标准模板库 定义: c++引入的一个标准类库 特点:1)数据结构和算法的 c++实现( 采用模板类和模板函数)2)数据的存储和算 ...

  3. Grid++Report应用(引入项目中)

    1.将Grid++Report安装文件中(\WebSamples\asp.net(csharp)\App_Code)的ReportData.cs,MssqlReportData.cs两个文件复制到自己 ...

  4. 洛谷 - P2257 - YY的GCD - 莫比乌斯反演 - 整除分块

    https://www.luogu.org/problemnew/show/P2257 求 \(n,m\) 中 \(gcd(i,j)==p\) 的数对的个数 求 $\sum\limits_p \sum ...

  5. Codeforces714B【读题-水】

    题意: 给你n个数,然后让你自己选择X,先选择一些+一次:然后选择一些-一次. 思路: 首先要去判断是不是不需要处理或者处理一次的情况: 其实这样的话,你不可能选x然后最小和最大都加减一次,所以肯定是 ...

  6. Pycharm 配置autopep8到菜单

    Pycharm 可以自动检测PEP8规范. 我们可以安装autopep8来自动修改文件实现PEP8规范. 1.通过Pycharm安装autopep8 2.File->Setting->Ex ...

  7. Hackintosh

    条件:Mac环境(也可在Windows电脑上用虚拟机建立).两只(一只亦可)16G及以上优盘.一块64G以上SSD固态(机械)硬盘.一台待折腾的Windows电脑 1.创建安装盘: ·app stor ...

  8. 自然语言处理(三)——PTB数据的batching方法

    参考书 <TensorFlow:实战Google深度学习框架>(第2版) 从文本文件中读取数据,并按照下面介绍的方案将数据整理成batch. 方法是:先将整个文档切分成若干连续段落,再让b ...

  9. OSPF-1-OSPF的数据库交换(1)

    一.OSPF路由器ID(RID) 选举过程: 1.使用router-id id 命令中配置的路由器ID 2.up着的环回接口最大的ip 3.up着的非环回接口最大ip   如果路由器的RID发生了变化 ...

  10. java操作mongodb数据库实现新建数据库,新建集合,新建文档

    *首先明确一点,要通过java代码创建mongodb数据库实例,需要同时创建集合和文档. 代码实现: /* 建立与mongodb数据库的连接,可指定参数,如:MongoClient client = ...