leetcode200
深度优先搜索,每次遇到1,则岛的数量+1,从这个1开始找到所有相连的1,将其改为0。
public class Solution
{
private void dfsSearch(char[,] grid, int i, int j, int rows, int cols)
{
if (i < || i >= rows || j < || j >= cols)
{
return;
}
if (grid[i, j] == '')
{
return;
}
grid[i, j] = '';
dfsSearch(grid, i + , j, rows, cols);
dfsSearch(grid, i - , j, rows, cols);
dfsSearch(grid, i, j + , rows, cols);
dfsSearch(grid, i, j - , rows, cols);
} public int NumIslands(char[,] grid)
{
var rows = grid.GetLength();
var cols = grid.GetLength();
if (rows == || cols == )
{
return ;
} int count = ;
for (int i = ; i < rows; i++)
{
for (int j = ; j < cols; j++)
{
if (grid[i, j] == '')
{
count++;
dfsSearch(grid, i, j, rows, cols);
}
}
}
return count;
}
}
leetcode200的更多相关文章
- Java 图的遍历-LeetCode200
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [Swift]LeetCode200.岛屿的个数 | 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 深度优先+广度优先】 岛屿数量
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- Leetcode200. Number of Islands岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- leetcode200 Number of Islands
""" Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ...
- LeetCode200 岛屿的个数
给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. 示例 1: 输入: ...
- 并查集算法Union-Find的思想、实现以及应用
并查集算法,也叫Union-Find算法,主要用于解决图论中的动态连通性问题. Union-Find算法类 这里直接给出并查集算法类UnionFind.class,如下: /** * Union-Fi ...
随机推荐
- 20165326 java实验四
20165326实验四-Android程序设计 一:Android Stuidio的安装测试 1.安装Android Stuidio 具体跟着教程走就行主要是配置的时候要选择下载SDK或手动配置,详细 ...
- nginx 开启高效文件传输模式
(1) sendfile 参数用于开启文件的高效传输模式,该参数实际上是激活了 sendfile() 功能,sendfile() 是作用于两个文件描述符之间的数据拷贝函数,这个拷贝操作是在内核之中的, ...
- scrapy中crawlspide中callback和follow函数的作用及使用方法
Rule(LinkExtractor(allow=r'i/tems'),callback='parse_item',follow=True) 当前代码的含义就是将当前页面及按照allow=r'i/t ...
- offsetWidth与clientWidth 区别
offsetWidth //元素宽度.内边距和边框,不包括外边距 offsetHeight //元素高度.内边距和边框,不包括外边距 clientWidth //元 ...
- 剑指Offer 31. 整数中1出现的次数(从1到n整数中1出现的次数) (其他)
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- 第一篇,java学习之旅
在java的这座殿堂中,我才刚刚推开了大门,就像是在岔路口找到了一条,走向前进java大门的路. 下面是一些java算法的问题 第一题: package project.model; import j ...
- Js 基本类型和引用类型
一个变量可以存放两种类型的值,基本类型的值(primitive values)和引用类型的值(reference values). ES6 引入了一种新的原始数据类型 Symbol,表示独一无二的值. ...
- Day 1: ASP.NET and JavaScript Jan.16th Trying
ASP.NET has its own named controls(tags) corresponding to that in an HTML document, such as <asp: ...
- 3.1 unittest简介
3.1 unittest简介 前言 熟悉java的应该都清楚常见的单元测试框架Junit和TestNG.python里面也有单元测试框架-unittest,相当于是一个python版的junit.py ...
- yii2 自带分页使用
//下面为控制器层的方法内容use \yii\db\Query;use \yii\data\Pagination; //方法内容 $query=new Query();//from为自带 ...