1. Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
  2.  
  3. Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
  4.  
  5. Example 1:
  6. [[0,0,1,0,0,0,0,1,0,0,0,0,0],
  7. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  8. [0,1,1,0,1,0,0,0,0,0,0,0,0],
  9. [0,1,0,0,1,1,0,0,1,0,1,0,0],
  10. [0,1,0,0,1,1,0,0,1,1,1,0,0],
  11. [0,0,0,0,0,0,0,0,0,0,1,0,0],
  12. [0,0,0,0,0,0,0,1,1,1,0,0,0],
  13. [0,0,0,0,0,0,0,1,1,0,0,0,0]]
  14. Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
  15. Example 2:
  16. [[0,0,0,0,0,0,0,0]]
  17. Given the above grid, return 0.
  18. Note: The length of each dimension in the given grid does not exceed 50.

BFS + 涂色法:

  1. class Solution {
  2. public int maxAreaOfIsland(int[][] grid) {
  3. if(grid == null){
  4. return 0;
  5. }
  6. int row = grid.length;
  7. int column = grid[0].length;
  8. int max = 0;
  9. for(int i = 0; i<row; i++){
  10. for(int j = 0; j<column; j++){
  11. if(grid[i][j] == 1){
  12. //count++;
  13. //grid[i][j] = 2;
  14. Queue<int[]> queue = new LinkedList<>();
  15. int count = 0;
  16. queue.add(new int[]{i,j});
  17. grid[i][j] = 2;
  18. while(!queue.isEmpty()){
  19. int[] temp = queue.poll();
  20. int r = temp[0];
  21. int c = temp[1];
  22. count++;
  23. //move up
  24. if(r - 1 >= 0 && grid[r - 1][c] == 1){
  25. queue.add(new int[]{r-1, c});
  26. grid[r-1][c] = 2;
  27. }
  28. //move down
  29. if(r + 1 < row && grid[r + 1][c] == 1){
  30. queue.add(new int[]{r+1, c});
  31. grid[r+1][c] = 2;
  32. }
  33. //move left
  34. if(c - 1 >= 0 && grid[r][c - 1] == 1){
  35. queue.add(new int[]{r, c-1});
  36. grid[r][c-1] = 2;
  37. }
  38. //move right
  39. if(c + 1 < column && grid[r][c + 1] == 1){
  40. queue.add(new int[]{r, c+1});
  41. grid[r][c+1] = 2;
  42. }
  43. }
  44. if(count>max){
  45. max = count;
  46. }
  47. }
  48.  
  49. }
  50. }
  51. return max;
  52. }
  53. }

LeetCode - Max Area of Island的更多相关文章

  1. [LeetCode] Max Area of Island 岛的最大面积

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  2. Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island)

    Leetcode之深度优先搜索(DFS)专题-695. 岛屿的最大面积(Max Area of Island) 深度优先搜索的解题详细介绍,点击 给定一个包含了一些 0 和 1的非空二维数组 grid ...

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

  4. [leetcode]python 695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. C#LeetCode刷题之#695-岛屿的最大面积( Max Area of Island)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3736 访问. 给定一个包含了一些 0 和 1的非空二维数组 gr ...

  6. 【leetcode】Max Area of Island

    国庆中秋长假过完,又要开始上班啦.先刷个题目找找工作状态. Given a non-empty 2D array grid of 0's and 1's, an island is a group o ...

  7. LeetCode 695. Max Area of Island (岛的最大区域)

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  8. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

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

  9. [Leetcode]695. Max Area of Island

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

随机推荐

  1. 基于Redis+MySQL+MongoDB存储架构应用

    摘  要: Redis+MySQL+MongoDB技术架构实现了本项目中大数据存储和实时云计算的需求.使用MongoDB切片的水平动态添加,可在不中断平台业务系统的同时保障扩容后的查询速度和云计算效能 ...

  2. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  3. svn服务器搭建及使用(三)

    接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...

  4. TreeMap - 源代码学习笔记

    TreeMap 实现了 NavigableMap 接口,而NavigableMap 接口继承于 SortedMap接口. 所有本文还会记录 SortedMap 和 NavigableMap 的阅读笔记 ...

  5. Python发送四种格式的post请求的代码样例

    x-www-form-urlencoded: path = "/api/v1/topics/update"params={'accesstoken':'d38a77f0-6f29- ...

  6. fortran学习网站

    https://www.sciencesoft.cn/Fortran/Index.htm http://m.fcode.cn/

  7. netty源码理解(二) serverstrap.bind()

    eventloop是一个线程,里面有一个executor封装了一个线程工厂,在启动的时候启动一个线程,传入的实现了runnable的内部类,里面调用了eventloop的run方法.

  8. Python 嵌套

    1 要yuanhao的首字母大写 li = [1,2,5,'taibai','yuanhao',[1,'alex',3,],True]li[4] = 'Yuanhao'print(li) li[4] ...

  9. chromium ②

    这篇研究两个问题:chromium对线程的封装和进程通信.主要参考chromium的官方技术文档:Treading和Inter-process Communication (IPC). chrome速 ...

  10. 客户端优化之使用javascript原生方法替代复杂的数学运算和jquery方法

    尽管jQuery等js框架相比原生javascript使用起来极为方便但是为什么在一些大型互联网公司还是一致强调前端开发人员的js基础,因为尽管javascript使用起来可能非常不便不仅体现在语法而 ...