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 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.
分析
题目描述抽象为一个图的问题,题目本质便是求连通子图的数目。
借助图的遍历算法DFS的思想,遍历该二维矩阵,每当遇到一个‘1’计数增一,同时以该坐标为起点dfs该矩阵把相邻坐标为‘1’的元素改为非1;最终计数的结果即是连通子图数量。
AC代码
class Solution {
public:
//等价于计算连通子图的个数
int numIslands(vector<vector<char>>& grid) {
if (grid.empty())
return 0;
//计算该二维数组的行列
int rows = grid.size();
int cols = grid[0].size();
int count = 0;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
if (grid[i][j] == '1')
{
++count;
dfs(grid, i, j);
}
continue;
}//for
}//for
return count;
}
void dfs(vector<vector<char>> &grid, int r, int c)
{
if (grid.empty())
return;
//计算该二维数组的行列
int rows = grid.size();
int cols = grid[0].size();
if (r < 0 || r >= rows || c < 0 || c >= cols)
return;
if (grid[r][c] == '1')
{
//改变当前元素值为非'1'
grid[r][c] = '2';
dfs(grid, r, c + 1);
dfs(grid, r + 1, c);
dfs(grid, r, c - 1);
dfs(grid, r - 1, c);
}//if
return;
}
};
LeetCode(200) Number of Islands的更多相关文章
- Leetcode(4)寻找两个有序数组的中位数
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
随机推荐
- 从两个不同的ServiceProvider说起
从两个不同的ServiceProvider说起 我们一致在说 ASP.NET Core广泛地使用到了依赖注入,通过前面两个系列的介绍,相信读者朋友已经体会到了这一点.由于前面两章已经涵盖了依赖注入在管 ...
- F. Coprime Subsequences 莫比乌斯反演
http://codeforces.com/contest/803/problem/F 这题正面做了一发dp dp[j]表示产生gcd = j的时候的方案总数. 然后稳稳地超时. 考虑容斥. 总答案数 ...
- linux打包文件,压缩文件
1.打包: linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进行压缩. 1.命令格 ...
- mysql非常全的和完整的总结
(1)数据类型 类型 备注 tinyint/smallint/mediumint/int/bigint 1B/2B/3B/4B/8B float/double 单精度/双精度浮点型 decimal 不 ...
- 从0到1分步实现一个出生日期的正则表达式(JavaScript)
简言 在表单验证中,经常会用正则表达式做出生日期校验.本文把出生日期分割成几个部分,分步地介绍了实现一个出生日期校验的完整过程.相信您在理解了本篇的内容后,对如何编写和如何应用正则表达式会有进一步的理 ...
- cf519D. A and B and Interesting Substrings(前缀和)
题意 给出$26$个字母对应的权值和一个字符串 问满足以下条件的子串有多少 首尾字母相同 中间字母权值相加为0 Sol 我们要找到区间满足$sum[i] - sum[j] = 0$ $sum[i] = ...
- markdown-Macdown
#标题 [页面锚点](#name) => <a name="name"></a>文字 **加粗**(Command-B) *斜体*(Comma ...
- echarts使用中的那些事儿(二)
然而第二天的事情你怎么能猜到客户的心思呢: 客户:右边的是啥? 偶:可放大缩小查看各个时期的数据. 客户:把它去掉吧,全部直观显示. 偶:哦,不过数据过多的话会导致页面过长的. 客户:只有你知道这个可 ...
- idea2018.1.2 激活
https://www.cnblogs.com/ycjcham/p/8724451.html
- 从程序猿到SAP产品经理,我是如何转型的?
文章作者:Jason Xia(夏建军) Jerry: 今天的文章来自Jason Xia, 我的老同事,和我一样从2007年进入SAP成都研究院工作至今.这篇文章讲述了Jason是如何从一名SAP资深开 ...