题目

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;
}
};

GitHub测试程序源码

LeetCode(200) Number of Islands的更多相关文章

  1. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  2. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  3. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  4. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  5. 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 ...

  6. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. 练习三十二:用python实现:按相反的顺序输出列表的每一位值

    用python实现:按相反的顺序输出列表的每一位值 1. 使用list[::-1] list1 = ["one","two","three" ...

  2. .NET Core使用NLog通过Kafka实现日志收集

    微服务日志之.NET Core使用NLog通过Kafka实现日志收集 https://www.cnblogs.com/maxzhang1985/p/9522017.html 一.前言 NET Core ...

  3. input文本框默认提示

    今天闲暇时间把自己以前写的一个文本框默认提示函数改成了一个小插件.下面是代码 1.引入jQuery库 <script src="http://code.jquery.com/jquer ...

  4. PHP+Mysql+jQuery找回密码

    通常所说的密码找回功能不是真的能把忘记的密码找回,因为我们的密码是加密保存的,一般开发者会在验证用户信息后通过程序生成一个新密码或者生成一个特定的链接并发送邮件到用户邮箱,用户从邮箱链接到网站的重置密 ...

  5. 网络编程之异步IO,rabbitMQ笔记

    对于网络并发编程而言,多线程与多进程算是最常见的需求场景了.毕竟网站开放就是想要更多的流量访问的. 回顾 回顾下之前学过的关于线程,进程和协程的知识点 IO密集型任务--用多线程更好计算密集型任务-- ...

  6. JavaScript 函数(方法)

    1 定义 1.1 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. 语法: 函数就是包裹在大括号中的代码块,前面使用了关键词 function function 方法名(参数列表){ 代码 ...

  7. SharePoint Online 缺少“将站点另存为模板”

    之前文章行给出在SharePoint 2010 .SharePoint 2013 中将站点保存模板选项的文章.其实同样的问题出现在Microsoft Office 365的一部分SharePoint ...

  8. 重温Javascript(一)-基本概念

    工作中要用到JavaScript,一组复习笔记. 一些看法 1. 想想JavaScript目前最常用的宿主环境,浏览器或者服务端V8,都是单线程,所以不用过多的考虑并发的问题,如果是协程来实现异步的方 ...

  9. 如何在程序中加入Growl通知

    Growl for Windows – Mac 样式的信息提示工具.目前已经支持的软件包括:Outlook,Visual Studio 等以及一个利用命令行从本地或者远程发送消息过来的工具 .Grow ...

  10. mvc的help和functions语法

    @helper show(int num ) { ) { @:存在 } else { @:不存在 } } @functions { /// <summary> /// 方法必须要求为静态 ...