LeetCode 200. Number of Islands 岛屿数量(C++/Java)
题目:
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)的更多相关文章
- [leetcode]200. Number of Islands岛屿数量
dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...
- [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 ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- [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 ...
- 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 用了第一种方式, ...
- [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 ...
- 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 ...
- (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 ...
- 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 ...
随机推荐
- linux dubbo-admin-2.6.0 环境搭建
1.去maven官网下载apache-maven-3.6.2-bin.tar.gz安装包 2.上传至linux服务器中 3.解压maven安装包 tar -zxvf apache-maven-3.6. ...
- Clover软件使用中遇到的问题
安装Clover应用后不生效,Win + E 时仍然默认打开系统的资源管理器 解决: 进入ie浏览器的管理加载项窗口,显示栏选择所有加载项 找到 Explorer Watcher Class 项,确保 ...
- SQL练习题(一)
目录 题目一:交换性别(简单) 实现思路 提交代码 题目二:连续出现的数字(中等) 实现思路 方式一 方式二 提交代码 方式一 方式二 题目三:换座位(中等) 实现思路 方式一 方式二 提交代码 方式 ...
- bash的默认组合键
组合键 组合按键 执行结果 Ctrl+C 终止目前的命令 Ctrl+D 输入结束(EOF),例如邮件结束的时候 Ctrl+M 就是Enter啦! Ctrl+S 暂停屏幕输出 Ctrl+Q 恢复屏幕输出 ...
- MySQL数据库(五)插入操作
前提要述:参考书籍<MySQL必知必会> <MySQL必知必会>是先讲了查询,但是没有记录就无法查询,所以先将如何添加数据. 表已经知道怎么创建了,随便创两张. 5.1 插入数 ...
- 通过ArcGIS将数据存储到SQL Server2012中
一.软件安装: ARCGIS 10.3安装 SQLserver2012安装 ARCGIS 10.3 安装(注意ARCGIS10.3并不用安装配置ARCSDE). https://wenku.baidu ...
- Eclipse常用快捷键【转】
Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键.1. [ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的 ...
- numpy 读取数据
一.CSV文件 CSV: Comma-Separated Value,逗号分隔值文件 显示:表格状态 源文件:换行和逗号分隔,逗号 列,换行 行 二.读取数据 1.方法 loadtxt(fname, ...
- matplotlib 散点图
一.特点 离散的数据,查看分布规律,走向趋势 二.使用 1.核心 plt.scatter(x, y) # x为x轴的数据,可迭代对象,必须是数字 # y为y轴的数据,可迭代对象,必须是数字 # x和y ...
- Java入门 - 语言基础 - 14.String类
原文地址:http://www.work100.net/training/java-string.html 更多教程:光束云 - 免费课程 String类 序号 文内章节 视频 1 概述 2 创建字符 ...