Leetcode算法题:695. 岛屿的最大面积

一共有3种方法

  1. 递归方法 + 深度优先遍历
//递归实现深度优先遍历
public int dfs(int[][] grid, int i, int j){
    int area = 0;
    
    //上下左右四个移动方向
    int[][] move_array = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
    
    //数组越界或者该点为0,就返回0
    if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] == 0)
        return  0;
    
    //访问过的点就置0
    grid[i][j] = 0;
    ++area;

    for(int[] x : move_array){
        area += dfs(grid, i + x[0], j + x[1]);
    }

    return area;
}

public int maxAreaOfIsland(int[][] grid) {
    int max = 0;
    int m = grid.length;
    int n = grid[0].length;

    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            max = Math.max(max, dfs(grid, i, j));
        }
    }

    return max;
}

2. 栈 + 深度优先遍历非常重要,要加深理解

//栈方法实现深度优先遍历
public static int maxAreaOfIsland(int[][] grid) {
    int max = 0;
    int m = grid.length;
    int n = grid[0].length;
    //用栈实现深度优先遍历
    Deque<int[]> stack = new LinkedList<>();
    //上下左右四个移动方向
    int[][] move_array = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            stack.add(new int[]{i, j});
            int area = 0;

            while (!stack.isEmpty()){
                int x = stack.peek()[0];
                int y = stack.pop()[1];

                //数组越界或者该点为0,就继续下一个循环
                if(x < 0 || y < 0 || x >= m || y >= n || grid[x][y] == 0)
                    continue;

                //访问过的点就置0
                grid[x][y] = 0;
                ++area;

                //上下左右四个方向都要访问
                for(int[] move : move_array){
                    stack.add(new int[]{x + move[0], y + move[1]});
                }
            }

            max = Math.max(max, area);
        }
    }

    return max;
}
  1. 队列 + 广度优先遍历
//队列方法实现广度优先遍历
public int maxAreaOfIsland(int[][] grid) {
    int max = 0;
    int m = grid.length;
    int n = grid[0].length;
    //用队列实现广度优先遍历
    Deque<int[]> queue = new LinkedList<>();
    //上下左右四个移动方向
    int[][] move_array = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            queue.addLast(new int[]{i, j});
            int area = 0;

            while (!queue.isEmpty()){
                int size = queue.size();
                
                for(int t = 0; t <size; t++){
                    int x = queue.peekFirst()[0];
                    int y = queue.removeFirst()[1];

                    //数组越界或者该点为0,就继续下一个循环
                    if(x < 0 || y < 0 || x >= m || y >= n || grid[x][y] == 0)
                        continue;

                    //访问过的点就置0
                    grid[x][y] = 0;
                    ++area;

                    //上下左右四个方向都要访问
                    for(int[] move : move_array){
                        queue.add(new int[]{x + move[0], y + move[1]});
                    }
                }
            }

            max = Math.max(max, area);
        }
    }

    return max;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值