题目:

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变成0),然后继续遍历到结尾即可。

程序:

C++

class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == || grid[].size() == )
return ;
m = grid.size();
n = grid[].size();
int res = ;
for(int i = ; i < m; ++i){
for(int j = ; j < n; ++j){
if(grid[i][j] == ''){
dfs(grid, i, j);
res++;
}
}
}
return res;
}
private:
void dfs(vector<vector<char>>& grid, int i, int j){
if(i < || j < || i >= m || j >= n || grid[i][j] == '')
return;
grid[i][j] = '';
dfs(grid, i-, j);
dfs(grid, i, j-);
dfs(grid, i+, j);
dfs(grid, i, j+);
}
int m;
int n;
};

Java

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

LeetCode 200. Number of Islands 岛屿数量(C++/Java)的更多相关文章

  1. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

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

  3. 【LeetCode】200. Number of Islands 岛屿数量

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

  4. [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 ...

  5. leetcode 200. Number of Islands 、694 Number of Distinct Islands 、695. Max Area of Island 、130. Surrounded Regions

    两种方式处理已经访问过的节点:一种是用visited存储已经访问过的1:另一种是通过改变原始数值的值,比如将1改成-1,这样小于等于0的都会停止. Number of Islands 用了第一种方式, ...

  6. [LeetCode] 0200. Number of Islands 岛屿的个数

    题目 Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is su ...

  7. Java for 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 ...

  8. (BFS/DFS) 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 ...

  9. 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 ...

随机推荐

  1. 安装Docker Machine

    什么是Docker Machine Docker Machine是Docker官方编排项目之一,由Go语言实现,负责在多种平台上快速安装Docker环境,Github项目主页 它支持Linux.Mac ...

  2. Http请求头安全策略

    今天在网上浪了许久,只是为了找一个很简单的配置,却奈何怎么都找不到. 好不容易找到了,我觉得还是记录下来的好,或许省得许多人像我一样浪费时间. 1.X-Frame-Options 如果网站可以嵌入到I ...

  3. Codeforces Round #604 (Div. 2) E. Beautiful Mirrors 题解 组合数学

    题目链接:https://codeforces.com/contest/1265/problem/E 题目大意: 有 \(n\) 个步骤,第 \(i\) 个步骤成功的概率是 \(P_i\) ,每一步只 ...

  4. getopt命令

    最近学习了一下getopt(不是getopts)命令来处理执行shell脚本传入的参数,在此记录一下,包括长选项.短选项.以及选项的值出现的空格问题,最后写了个小的脚本来处理输入的参数 首先新建一个t ...

  5. java序列化(一)

    今天我们来探讨一下java的序列化与反序列化.之前对此一直有概念,但是并没有真正的去测试.大家都知道,所谓的序列化就是把java代码读取到一个文件中,反序列化就是从文件中读取出对象.在网络传输过程中, ...

  6. 变量的取用与设定:echo,变量设定规则,unset

    1.变量的取用echo echo $variable echo ${variable} 2.变量的设定规则 3.让我设定的name=VBird应用在下个应用程序 4.进入到核心的模块目录 5.取消设定 ...

  7. 【一起学源码-微服务】Eureka+Ribbon+Feign阶段性总结

    前言 想说的话 这里已经梳理完Eureka.Ribbon.Feign三大组件的基本原理了,今天做一个总结,里面会有一个比较详细的调用关系流程图. 说明 原创不易,如若转载 请标明来源! 博客地址:一枝 ...

  8. Netty快速入门(08)ByteBuf组件介绍

    前面的内容对netty进行了介绍,写了一个入门例子.作为一个netty的使用者,我们关注更多的还是业务代码.也就是netty中这两种组件: ChannelHandler和ChannelPipeline ...

  9. echarts设置网格线颜色

    xAxis: { type: 'value', //设置网格线颜色 splitLine: { show: true, lineStyle:{ color: ['#315070'], width: 1, ...

  10. Appium自动化测试框架研究(2)——搭建IOS环境

    今天的文章讲iOS的Appium环境搭建. 对于iOS而言,只能在Mac笔记本上安装Appium,以及所需要的各种组件. 也许有人会问,能否在Windows系统上使用Appium测试iOS手机,这不就 ...