Leetcode 695. Max Area of Island

版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://blog.csdn.net/Quincuntial/article/details/83419075

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Max Area of Island

2. Solution

class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        if(grid.size() == 0) {
            return 0;
        }
        int area = 0;
        int max_area = 0;
        int rows = grid.size();
        int columns = grid[0].size();
        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < columns; j++) {
                if(grid[i][j]) {
                    area = 0;
                    getArea(grid, i, j, area, rows, columns);
                    max_area = max(area, max_area);
                }
            }
        }
        return max_area;
    }

private:
    void getArea(vector<vector<int>>& grid, int i, int j, int& area, int& rows, int& columns) {
        if(i < 0 || i == rows || j < 0 || j == columns || grid[i][j] == 0) {
            return;
        }
        grid[i][j] = 0;
        area++;
        getArea(grid, i + 1, j, area, rows, columns);
        getArea(grid, i - 1, j, area, rows, columns);
        getArea(grid, i, j + 1, area, rows, columns);
        getArea(grid, i, j - 1, area, rows, columns);
    }
};

Reference

  1. https://leetcode.com/problems/max-area-of-island/description/
阅读更多

扫码向博主提问

SnailTyan

博客专家

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • 深度学习
  • PyTorch
  • OCR
  • Docker
  • Caffe
去开通我的Chat快问
换一批

没有更多推荐了,返回首页