zoukankan      html  css  js  c++  java
  • 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) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

    Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)

    Example 1:

    [[0,0,1,0,0,0,0,1,0,0,0,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,1,1,0,1,0,0,0,0,0,0,0,0],
     [0,1,0,0,1,1,0,0,1,0,1,0,0],
     [0,1,0,0,1,1,0,0,1,1,1,0,0],
     [0,0,0,0,0,0,0,0,0,0,1,0,0],
     [0,0,0,0,0,0,0,1,1,1,0,0,0],
     [0,0,0,0,0,0,0,1,1,0,0,0,0]]
    
    Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.

    Example 2:

    [[0,0,0,0,0,0,0,0]]
    Given the above grid, return 0.

    Note: The length of each dimension in the given grid does not exceed 50

    给定一个非空的二维阵列网格为0和1,一个岛是一组1(代表陆地)四方向(水平或垂直)。您可以假设网格的所有四个边缘被水包围。 在给定的2D数组中找到一个岛的最大面积。 (如果没有岛,最大面积为0.)

    解法:用广度优先法遍历值为1的坐标,用set保存已遍历过的坐标

    1. var maxAreaOfIsland = function (grid) {
    2. let max = 0;
    3. let posSet = new Set();
    4. for (let r = 0; r < grid.length; r++) {
    5. let row = grid[r];
    6. for (let c = 0; c < row.length; c++) {
    7. if (row[c] === 0 || posSet.has(getPosStr(r, c))) continue;
    8. let block = 0;
    9. let queue = [{ r, c }];
    10. posSet.add(getPosStr(r, c));
    11. while (queue.length > 0) {
    12. block++;
    13. let pos = queue.shift();
    14. let posRow = pos.r;
    15. let posCol = pos.c;
    16. if (posRow >= 1 && grid[posRow - 1][posCol] == 1 && !posSet.has(getPosStr(posRow - 1, posCol))) {
    17. queue.push({ r: posRow - 1, c: posCol });
    18. posSet.add(getPosStr(posRow - 1, posCol));
    19. }
    20. if (posRow < grid.length - 1 && grid[posRow + 1][posCol] == 1 && !posSet.has(getPosStr(posRow + 1, posCol))) {
    21. queue.push({ r: posRow + 1, c: posCol });
    22. posSet.add(getPosStr(posRow + 1, posCol));
    23. }
    24. if (posCol >= 1 && grid[posRow][posCol - 1] == 1 && !posSet.has(getPosStr(posRow, posCol - 1))) {
    25. queue.push({ r: posRow, c: posCol - 1 });
    26. posSet.add(getPosStr(posRow, posCol - 1));
    27. }
    28. if (posCol < row.length - 1 && grid[posRow][posCol + 1] == 1 && !posSet.has(getPosStr(posRow, posCol + 1))) {
    29. queue.push({ r: posRow, c: posCol + 1 });
    30. posSet.add(getPosStr(posRow, posCol + 1));
    31. }
    32. }
    33. max = Math.max(max, block);
    34. }
    35. }
    36. return max;
    37. };
    38. let getPosStr = (x, y) => {
    39. return x + "," + y;
    40. }






  • 相关阅读:
    关于Mobx中装饰器语法的环境配置
    关于Java单例模式中懒汉式和饿汉式的两种类创建方法
    Java学习笔记之异常处理
    create-react-app脚手架的安装和目录结构介绍
    关于win10下JDK环境变量的配置以及关于JDK的一些说明
    关于Android studio的安装和配置问题
    关于通过ServletContext获取数据出现的http500的错误的解决方案
    关于form表单提交到Servlet的时候出现tomcat启动错误的解决方法
    Android网络编程系列之Volley总结
    Android网络编程系列之HTTP协议原理总结
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/7638781.html
Copyright © 2011-2022 走看看