695. Max Area of Island
来源:互联网 发布:gt610疯牛病版 知乎 编辑:程序博客网 时间:2024/06/06 02:40
dfs遍历操作,搜索然后覆盖
class Solution {public: int search(int i,int j, vector<vector<int>>& grid){ int sum=0; if(i<0||i>=grid.size()||j<0||j>=grid[0].size()||grid[i][j]==0) return sum; sum+=1; grid[i][j] = 0; if(i-1>=0&&grid[i-1][j]==1) sum+=search(i-1,j, grid); if(i+1<grid.size()&&grid[i+1][j]==1) sum+=search(i+1,j, grid); if(j-1>=0&&grid[i][j-1]==1) sum+=search(i,j-1, grid); if(j+1<grid[0].size()&&grid[i][j+1]==1) sum+=search(i,j+1, grid); return sum; } int maxAreaOfIsland(vector<vector<int>>& grid) { int maxArea = 0; for(int i=0;i<grid.size();i++) { for(int j=0;j<grid[0].size();j++) { if(grid[i][j]==1){ int area =search(i,j,grid); maxArea = maxArea>area?maxArea:area; } } } return maxArea; }};
阅读全文
0 0
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- LWC 53:695. Max Area of Island
- leetcode 695. Max Area of Island 解法
- [LeetCode] Algorithms-695. Max Area of Island
- DFS:695. Max Area of Island
- Leetcode 695. Max Area of Island
- LeetCode-695. Max Area of Island
- 695. Max Area of Island Difficulty: Easy
- LeetCode 695. Max Area of Island
- LeetCode-695. Max Area of Island
- [LeetCode] 695. Max Area of Island
- 【LeetCode】695. Max Area of Island
- PAT 1088. Rational Arithmetic (20) 分数模拟计算+字符串处理
- B. 在哈尔滨的寒风中
- R语言不平衡数据分类指南
- 一步一步的写出你自己的makefile文件
- 2017/12/08
- 695. Max Area of Island
- CMake+mingw-w64编译opencv-3.3.1
- 问题 O: 中庸之道(二)
- 广东工业大学自动化(自动控制原理)作者:Haven
- 一元钱一瓶水,两个空瓶可以换一瓶,三个盖儿可以换一瓶,20元最多能喝多少?
- Wannafly挑战赛5 A珂朵莉与宇宙【暴力枚举】
- C/C++得到系统启动项
- 问题 P: 中庸之道(三)
- PAT 1089. Insert or Merge (25) 插入排序+归并排序