题目:

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:

  1. Input:
  2. 11110
  3. 11010
  4. 11000
  5. 00000
  6.  
  7. Output: 1

Example 2:

  1. Input:
  2. 11000
  3. 11000
  4. 00100
  5. 00011
  6.  
  7. Output: 3

分析:

给定一个二维数组,其中由1(陆地)和0(水)组成,要求岛屿的数量,而岛屿则是陆地间上下左右相连后形成的,斜向的不算相连则构不成岛屿。

遍历整个数组,当元素为1时,岛屿数目加一,此时我们要把所有与这个位置相连的陆地全部变成水(1变成0),然后继续遍历到结尾即可。

程序:

C++

  1. class Solution {
  2. public:
  3. int numIslands(vector<vector<char>>& grid) {
  4. if(grid.size() == || grid[].size() == )
  5. return ;
  6. m = grid.size();
  7. n = grid[].size();
  8. int res = ;
  9. for(int i = ; i < m; ++i){
  10. for(int j = ; j < n; ++j){
  11. if(grid[i][j] == ''){
  12. dfs(grid, i, j);
  13. res++;
  14. }
  15. }
  16. }
  17. return res;
  18. }
  19. private:
  20. void dfs(vector<vector<char>>& grid, int i, int j){
  21. if(i < || j < || i >= m || j >= n || grid[i][j] == '')
  22. return;
  23. grid[i][j] = '';
  24. dfs(grid, i-, j);
  25. dfs(grid, i, j-);
  26. dfs(grid, i+, j);
  27. dfs(grid, i, j+);
  28. }
  29. int m;
  30. int n;
  31. };

Java

  1. class Solution {
  2. public int numIslands(char[][] grid) {
  3. if(grid.length == 0 || grid[0].length == 0)
  4. return 0;
  5. m = grid.length;
  6. n = grid[0].length;
  7. int res = 0;
  8. for(int i = 0; i < m; ++i){
  9. for(int j = 0; j < n; ++j){
  10. if(grid[i][j] == '1'){
  11. dfs(grid, i, j);
  12. res++;
  13. }
  14. }
  15. }
  16. return res;
  17. }
  18. private void dfs(char[][] grid, int i, int j){
  19. if(i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == '0')
  20. return;
  21. grid[i][j] = '0';
  22. dfs(grid, i-1, j);
  23. dfs(grid, i, j-1);
  24. dfs(grid, i+1, j);
  25. dfs(grid, i, j+1);
  26. }
  27. private int m;
  28. private int n;
  29. }

LeetCode 200. Number of Islands 岛屿数量(C++/Java)的更多相关文章

  1. [leetcode]200. Number of Islands岛屿数量

    dfs的第一题 被边界和0包围的1才是岛屿,问题就是分理出连续的1 思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿. /* 思路是:遍历二维数组,遇到1就把周围连续的1 ...

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

  3. 【LeetCode】200. Number of Islands 岛屿数量

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

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

  5. 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 用了第一种方式, ...

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

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

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

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

随机推荐

  1. 从零开始安装Redis 集群(Linux CenOS7)

    从零开始安装Redis 集群(Linux CenOS7) 使用ISO安装CentOS7虚拟机 配置静态IP(参考Mac VMware Fusion CentOS7配置静态IP) 安装vim [root ...

  2. 使用tushare 库查阅交易日历

    资料参考:https://tushare.pro/ 交易日历 接口:trade_cal描述:获取各大交易所交易日历数据,默认提取的是上交所 tushare的版本和更新: 执行命令: pip insta ...

  3. 微信公众号 唤醒手机导航APP 一看就懂 复制即用

    公司自研发框架,基本上没啥看不懂的 基本都是直接复制用就好了!希望能帮助到需要的朋友! 新建俩个同级文件用来保存 jsapi_ticket 和 access_token的文件 命名:jsapi_tic ...

  4. BZOJ 2038: [2009国家集训队]小Z的袜子 (莫队)

    题目传送门:小Z的袜子 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命…… ...

  5. 【记】VirtualBox安装CentOS6

    推荐随笔 VirtualBox中安装CentOS-6.6虚拟机 问题1: 在选择虚拟硬盘大小时,最好不要用默认的8G 我的分区 /boot 200M swap 1024M /home 4096M / ...

  6. 在idea中运行GitHub项目

    1.首先在本地建一个文件夹,比如qm 2. 在GitHub中找到你所运行项目的路径 3.在idea中[File]-->[New]--->[Project from Version Cont ...

  7. Spring Boot2 系列教程 (十六) | 整合 WebSocket 实现广播

    前言 如题,今天介绍的是 SpringBoot 整合 WebSocket 实现广播消息. 什么是 WebSocket ? WebSocket 为浏览器和服务器提供了双工异步通信的功能,即浏览器可以向服 ...

  8. Spring Boot2 系列教程(一) | 如何使用 IDEA 构建 Spring Boot 工程

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. Search 前言 新年立了个 flag,好好运营这个公众号.具体来说,就是每周要写两篇文章在这个号发表.刚立的 fla ...

  9. Java框架之Spring02-AOP-动态代理-AspectJ-JdbcTemplate-事务

    AOP 动态代理 代理设计模式的原理:使用一个代理将原本对象包装起来,然后用该代理对象”取代”原始对象.任何对原始对象的调用都要通过代理.代理对象决定是否以及何时将方法调用转到原始对象上. 代理模式的 ...

  10. dp - 逆序数序列

    对于一个数列{ai},如果有i<j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的 数列,可以很容易求出有多少个逆序对数.那么逆序对数为k的这样自然数数 ...