dfs的第一题

被边界和0包围的1才是岛屿,问题就是分理出连续的1

思路是遍历数组数岛屿,dfs四个方向,遇到1后把周围连续的1置零,代表一个岛屿。

  1. /*
  2. 思路是:遍历二维数组,遇到1就把周围连续的1变成0,res+1,然后继续遍历,直到结束
  3. 周围连续1置零用的是dfs,向四个位置搜索,遇到0返回
  4. */
  5. public int numIslands(char[][] grid) {
  6. if (grid.length==0) return 0;
  7. int r = grid.length;
  8. int c = grid[0].length;
  9. int res = 0;
  10. //遍历数组
  11. for (int i = 0; i < r; i++) {
  12. for (int j = 0; j < c; j++) {
  13. if (grid[i][j]=='1')
  14. {
  15. res++;
  16. //进行置零操作
  17. dfs(grid,i,j,r,c);
  18. }
  19. }
  20. }
  21. return res;
  22. }
  23. public void dfs(char[][] grid,int i,int j,int r,int c)
  24. {
  25. //出界和遇0返回
  26. if (i<0||j<0||i>=r||j>=c||grid[i][j]!='1') return;
  27. //置零
  28. grid[i][j]='0';
  29. //把周围连续的置零
  30. dfs(grid,i-1,j,r,c);
  31. dfs(grid,i+1,j,r,c);
  32. dfs(grid,i,j-1,r,c);
  33. dfs(grid,i,j+1,r,c);
  34. }

[leetcode]200. Number of Islands岛屿数量的更多相关文章

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

  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. ubuntu配置网络和静态路由(界面配置形式)

    目录 网卡配置 静态ip配置 静态路由 外网ip配置(动态获取DHCP) 内网ip和静态路由配置 本文主要针对ubuntu18.0系统进行界面形式配置网络.并配置静态路由. 网卡配置 静态ip配置 打 ...

  2. Alpha冲刺-第七次冲刺笔记

    Alpha冲刺-冲刺笔记 这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2 这个作业要求在哪里 https://edu.cnblogs. ...

  3. devc++编译时 undefined reference to `__imp_WSAStartup'

    socket编程时遇到的问题:

  4. kali查看本机ip

  5. 课堂笔记【java JDBC】

    目录 JDBC简介 工作原理: 工作过程: JDBC驱动与连接 JDBC驱动 连接JDBC驱动 1.下载特定数据库的JDBCjar包 2.加载并注册数据库驱动 3.连接驱动 JDBC常见API JDB ...

  6. PyQt(Python+Qt)学习随笔:Qt Designer中部件的mouseTracking和tabletTracking属性

    在Qt Designer中的部件属性设置中,有mouseTracking和tabletTracking两个属性,这两个属性是跟踪鼠标或平板触控笔的移动轨迹的. 一.mouseTracking mous ...

  7. Docker部署CTF综合性靶场,定时刷新环境

    部署如DVWA或upload-labs这类综合性靶场的时候,虽然是使用Docker环境,设置好权限后容器被击穿的问题不需要考虑,但担心部分选手修改了题目环境,比如一直XSS弹窗,所以想要编写脚本每天定 ...

  8. MySQL-索引分类及使用索引

    1.什么是索引? 索引:存储引擎用于快速找到记录的一种数据结构,默认使用B-Tree索引.索引是存储引擎层中实现.简单理解为:排好序的快速查找数据结构 索引的目的:提高数据查询的效率,优化查询性能,就 ...

  9. mysql游标cursor与for循环

    delimiter // create procedure p2() begin declare row_id int DEFAULT 0; declare row_num int DEFAULT 0 ...

  10. java中的反射(二)

    java中的反射(一):https://www.cnblogs.com/KeleLLXin/p/14060555.html 目录 一.反射 1.class类 2.访问字段 3.调用方法 4.调用构造方 ...