[leetcode]200. Number of Islands岛屿数量
dfs的第一题
被边界和0包围的1才是岛屿,问题就是分理出连续的1
思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿。
/*
思路是:遍历二维数组,遇到1就把周围连续的1变成0,res+1,然后继续遍历,直到结束
周围连续1置零用的是dfs,向四个位置搜索,遇到0返回
*/
public int numIslands(char[][] grid) {
if (grid.length==0) return 0;
int r = grid.length;
int c = grid[0].length;
int res = 0;
//遍历数组
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (grid[i][j]=='1')
{
res++;
//进行置零操作
dfs(grid,i,j,r,c);
}
}
}
return res;
}
public void dfs(char[][] grid,int i,int j,int r,int c)
{
//出界和遇0返回
if (i<0||j<0||i>=r||j>=c||grid[i][j]!='1') return;
//置零
grid[i][j]='0';
//把周围连续的置零
dfs(grid,i-1,j,r,c);
dfs(grid,i+1,j,r,c);
dfs(grid,i,j-1,r,c);
dfs(grid,i,j+1,r,c);
}
[leetcode]200. Number of Islands岛屿数量的更多相关文章
- 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 s ...
- [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 ...
随机推荐
- Spring源码解析之BeanFactoryPostProcessor(二)
上一章,我们介绍了在AnnotationConfigApplicationContext初始化的时候,会创建AnnotatedBeanDefinitionReader和ClassPathBeanDef ...
- FPGA PS2协议
VerilogHDL那些事儿_建模篇(黑金FPGA开发板配套教程) 作者:akuei2 说明:参照该书将部分程序验证学习一遍 学习时间:2014年5月3号 主要收获: 1. 对PS2接口有初步了解: ...
- AtomicInteger的addAndGet(int delta)与getAndAdd(int delta)有什么区别?
结论:区别仅在于返回的结果,修改的值是相同的,但是返回的值不同. 看一下源码注释 1 /** 2 * Atomically adds the given value to the current va ...
- 转2:Python字符编码详解
1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...
- Python 常用方法和模块的使用(time & datetime & os &random &sys &shutil)-(六)
1 比较常用的一些方法 1.eval()方法:执行字符串表达式,并返回到字符串. 2.序列化:变量从内存中变成可存储或传输到文件或变量的过程,可以保存当时对象的状态,实现其生命周期的延长,并且需要时可 ...
- sails框架结合mocha的测试环境搭建
一.环境结构 1.首先最底层是操作系统 2.其次在操作系统之上是Node.js的运行环境,和Database 3.再之上就是sail和mocha框架 二.环境搭建 1.首先需要Node.js的运行环境 ...
- LeetCode初级算法之数组:48 旋转图像
旋转图像 题目地址:https://leetcode-cn.com/problems/rotate-image/ 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: ...
- linux c++ 内存泄漏检测工具:AddressSanitizer(ASan)
1.介绍 AddressSanitizer(ASan),该工具为gcc自带,4.8以上版本均可以使用. 2.使用 编译的方式很简单,只需要添加 -fsanitize=address -g 即可,如 g ...
- ASP数据库连接方法语法总结
经常使用到有关数据库的操作.包括连接代码,SQL命令等等,又不曾刻意去记忆它们(我本人是不愿意去记这东东),所以常常在用到的时候又去查书本,翻来翻去.一些比较少用的数据库还不一定能顺利找到,所以现在把 ...
- tornado 网页提交内容 展示内容作业
s2.py import tornado.ioloop import tornado.web text_list=[] class MainHandler(tornado.web.RequestHan ...