LeetCode-695. Max Area of Island [C++][Java]

LeetCode-695. Max Area of Islandicon-default.png?t=M1L8https://leetcode.com/problems/max-area-of-island/

题目描述

You are given an m x n binary matrix grid. 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.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

Example 1:

Input: grid = [[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]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.

Example 2:

Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • grid[i][j] is either 0 or 1.

 

解题思路

【C++】

1. stack循环

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size(), n = m ? grid[0].size() : 0, local_area, area = 0, x, y;
        vector<int> direction{-1, 0, 1, 0, -1};
        for (int i=0; i<m; i++) {
            for (int j=0; j<n; j++) {
                if (grid[i][j]) {
                    local_area = 1;
                    grid[i][j] = 0;
                    stack<pair<int, int>> island;
                    island.push({i, j});
                    while (!island.empty()) {
                        auto [r, c] = island.top(); island.pop();
                        for (int k=0; k<4; ++k) {
                            x = r + direction[k], y = c + direction[k+1];
                            if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) {
                                grid[x][y] = 0;
                                ++local_area;
                                island.push({x, y});
                            }
                        }
                    }
                    area = max(area, local_area);
                }
            }
        }
        return area;
    }
};

2. class递归

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int result = 0, m = grid.size(), n = grid[0].size();
        Utils utils;
        for(int i=0; i<m; i++) 
            for(int j=0; j<n; j++) 
                if(grid[i][j] == 1) 
                    result=max(result, utils.dfs(grid, i, j, m, n));
        return result;
    }
    
    class Utils {
        public:
            int steps[4][2] = {{-1, 0}, {1,  0}, {0,  -1}, {0,  1}};
            int dfs(vector<vector<int>> &grid, int x, int y, int m, int n) {
                if (x < 0 || x >= m || y < 0 || y >= n || grid[x][y] == 0) {return 0;}
                int count = 1;
                grid[x][y] = 0;//注意,在递归前需要先把当前格子更新为0
                for (auto &step : steps) {count += dfs(grid, x+step[0], y+step[1], m, n);}
                return count;
            }
    };
};

3. func递归

class Solution {
public:
    int steps[4][2] = {{-1, 0}, {1,  0}, {0,  -1}, {0,  1}};
    inline bool IsInGrid(const vector<vector<int>> &grid, int i, int j) {
        return i >= 0 && j >= 0 && i < grid.size() && j < grid[i].size();
    }

    void FindAreaOfIsland(vector<vector<int>> &grid, int i, int j, int &area) {
        if (!grid[i][j]) {return;}
        grid[i][j] = 0;
        area++;
        for (int k = 0; k < 4; ++k) {
            int new_i = i + steps[k][0];
            int new_j = j + steps[k][1];
            if (!IsInGrid(grid, new_i, new_j) || !grid[new_i][new_j]) {continue;}
            FindAreaOfIsland(grid, new_i, new_j, area);
        }
        return ;
    }

    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int max_area = 0, area;
        for (int i = 0; i < grid.size(); ++i) {
            for (int j = 0; j < grid[i].size(); ++j) {
                if (!grid[i][j]) continue ;
                area = 0;
                FindAreaOfIsland(grid, i, j, area);
                if (area > max_area) max_area = area;
            }
        }
        return max_area;
    }
};

整理一下

class Solution {
    vector<int> direction{-1, 0, 1, 0, -1};
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        if (grid.empty() || grid[0].empty()) {return 0;}
        int max_area = 0;
        for (int i=0; i<grid.size(); i++) {
            for (int j=0; j<grid[0].size(); j++) {
                if (grid[i][j] == 1) {
                    max_area = max(max_area, dfs(grid, i, j));
                }
            }
        }
        return max_area;
    }
    
    int dfs(vector<vector<int>>& grid, int r, int c) {
        if (grid[r][c] == 0) {return 0;}
        grid[r][c] = 0;
        int x, y, area = 1;
        for (int i=0; i<4; i++) {
            x = r + direction[i], y = c + direction[i+1];
            if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) {
                area += dfs(grid, x, y);
            }
        }
        return area;
    }
};

再换种思路

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        if (grid.empty() || grid[0].empty()) {return 0;}
        int max_area = 0;
        for (int i=0; i<grid.size(); i++) {
            for (int j=0; j<grid[0].size(); j++) {
                if (grid[i][j] == 1) {
                    max_area = max(max_area, dfs(grid, i, j));
                }
            }
        }
        return max_area;
    }
    
    int dfs(vector<vector<int>>& grid, int r, int c) {
        if (r < 0 || r >= grid.size()
            || c < 0 || c >= grid[0].size() || grid[r][c] == 0) {return 0;}
        grid[r][c] = 0;
        return 1 + dfs(grid, r + 1, c) + dfs(grid, r - 1, c)
                 + dfs(grid, r, c + 1) + dfs(grid, r, c - 1);
    }
};

【Java】

class Solution {
    public int maxAreaOfIsland(int[][] grid) {
        if (grid.length == 0 || grid[0].length == 0) {return 0;}
        int max_area = 0;
        for (int i=0; i<grid.length; i++) {
            for (int j=0; j<grid[0].length; j++) {
                if (grid[i][j] == 1) {
                    max_area = Math.max(max_area, dfs(grid, i, j));
                }
            }
        }
        return max_area;
    }
    
    private int dfs(int[][]grid, int r, int c) {
        if (r < 0 || r >= grid.length
           || c < 0 || c >= grid[0].length || grid[r][c] == 0) {return 0;}
        grid[r][c] = 0;
        return 1 + dfs(grid, r + 1, c) + dfs(grid, r - 1, c)
                 + dfs(grid, r, c + 1) + dfs(grid, r, c - 1);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值