LeetCode 695. Max Area of Island (Java版; Medium)

welcome to my blog

LeetCode 695. Max Area of Island (Java版; Medium)

题目描述
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.
第一次做; 核心: 1)BFS
class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        if(grid==null || grid.length==0 || grid[0]==null || grid[0].length==0){
            return 0;
        }
        int n = grid.length, m = grid[0].length;
        // 记录已访问过的坐标
        boolean[][] flag = new boolean[n][m];
        // 最终结果, 初始值为0
        int max=0;
        //遍历矩阵
        for(int i=0; i<n; i++){
            for(int j=0; j<m; j++){
                //如果当前位置是0或者当前位置访问过, 则进入下一轮循环
                if(grid[i][j]==0 || flag[i][j]==true){
                    continue;
                }
                //当前位置是1, 并且没有访问过, 从这里开始向四周扩散, 返回扩散的最大面积
                int cur = bfs(grid, flag, i, j);
                max = Math.max(max, cur);
            }
        }
        return max;
    }

    // 用来表示上下左右四个移动方向
    private int[][] moves = {{-1,0}, {1,0}, {0,-1}, {0,1}};

    // 使用bfs进行扩散, 并返回扩散结束后占据的面积
    private int bfs(int[][] grid, boolean[][] flag, int i, int j){
        int n = grid.length, m = grid[0].length;
        // 扩散面积初始值是1, 因为grid[i][j]==1
        int res = 1;
        // 辅助bfs的队列
        LinkedList<int[]> queue = new LinkedList<>();
        queue.add(new int[]{i,j});
        flag[i][j] = true;
        // bfs主体循环
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            // 当前位置进行上下左右移动
            for(int[] move : moves){
                int x = cur[0]+move[0];
                int y = cur[1]+move[1];
                // 如果移动后的坐标不越界并且移动后的坐标对应的值为1, 同时还没有访问过, 说明该位置有效
                if(x>=0 && x<n && y>=0 && y<m && grid[x][y]==1 && flag[x][y]==false){
                    queue.add(new int[]{x, y});
                    flag[x][y] = true;
                    //扩散面积加1
                    res++;
                }
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值