题目:

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. 洛谷$P$3160 局部极小值 $[CQOI2012]$ 状压$dp$

    正解:状压$dp$ 解题报告: 传送门! 什么神仙题昂,,,反正我是没有想到$dp$的呢$kk$,,,还是太菜了$QAQ$ 首先看数据范围,一个4×7的方格,不难想到最多有8个局部极小值,过于显然懒得 ...

  2. 洛谷$P$1486 郁闷的出纳员 $[NOI2004]$ $splay$

    正解:$splay$ 解题报告: 传送门! 依然先考虑要呲呲些什么操作鸭$QwQ$ 其实就只要一个删除区间,一个查询第$k$大,还一个插入就欧克? 删除区间的话直接旋转下根什么的然后直接把子树删了就好 ...

  3. $Poj3017\ Cut\ The\ Sequence$ 单调队列优化$DP$

    Poj   AcWing Description 给定一个长度为N的序列 A,要求把该序列分成若干段,在满足“每段中所有数的和”不超过M的前提下,让“每段中所有数的最大值”之和最小. N<=10 ...

  4. $Poj2228$/洛谷$SP283\ Naptime$ 环形$DP$

    Luogu 一定要记得初始化为-inf!!! Description 在某个星球上,一天由N小时构成.我们称0-1点为第一个小时,1-2点为第二个小时,以此类推.在第i个小时睡觉能恢复Ui点体力.在这 ...

  5. Python3 虚拟开发环境搭建

    虚拟环境的搭建 优点 1.使不同应用开发环境相互独立 2.环境升级不影响其他应用,也不会影响全局的python环境 3.防止出现包管理混乱及包版本冲突 windows 安装 # 建议使用pip3安装到 ...

  6. 二、webdriver API

    目录 1. webdriver中常用属性 2. 浏览器页面操作 3. 鼠标操作 4. 键盘操作 5. 下拉框操作 1. webdriver中常用属性 import time from selenium ...

  7. 洛谷P1776 宝物筛选 题解 多重背包

    题目链接:https://www.luogu.com.cn/problem/P1776 题目大意: 这道题目是一道 多重背包 的模板题. 首先告诉你 n 件物品和背包的容量 V ,然后分别告诉你 n ...

  8. 「洛谷P1080」「NOIP2012提高组」国王游戏 解题报告

    P1080 国王游戏 题目描述 恰逢 \(H\)国国庆,国王邀请\(n\)位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 \( ...

  9. docker练习-堆栈

    介绍 分布式应用程序层次结构的顶部:堆栈. 堆栈是一组相互关联的服务,它们共享依赖关系,并且可以协调和缩放在一起. 单个堆栈能够定义和协调整个应用程序的功能(尽管非常复杂的应用程序可能希望使用多个堆栈 ...

  10. 26.python操作Excel

    写入Excel import xlwt book=xlwt.Workbook(encoding='utf-8') sheet=book.add_sheet('xiangxin') # 标题名 titl ...