leetcode 695. Max Area of Island——第一次成功的dfs
来源:互联网 发布:圣剑网络游戏厅 编辑:程序博客网 时间:2024/06/04 21:33
题目倒是很简单,思路也很简单,我的第一反应就是dfs。最近看了不少dfs的题目和解答,但是实操却不行,没想到这个倒是让我成功了。
我刚刚开始刷题,这是array里面简单程度排名前五的题。。。
上代码:
class Solution {public: int maxAreaOfIsland(vector<vector<int>>& grid) { int ans = 0, tmp = 0; for(int i = 0; i < grid.size(); i++) for(int j = 0; j < grid[0].size(); j++){ if(grid[i][j] == 1) tmp = dfs(grid, i, j); if(tmp > ans) ans =tmp; } return ans; } int dfs(vector<vector<int>>& grid, int i, int j){ if( i >= 0 && i < grid.size() && j >= 0 && j < grid[0].size() && grid[i][j] == 1){ grid[i][j] = 0; return 1 + dfs(grid, i+1, j) + dfs(grid, i-1, j) + dfs(grid, i, j-1) + dfs(grid, i, j+1); } return 0; }};
代码写的不够漂亮,但是目前水平大概就只有这样了。
第一次写博客感觉还不错!
望共勉,同进步!
阅读全文
0 0
- leetcode 695. Max Area of Island——第一次成功的dfs
- leetcode 695. Max Area of Island 深度优先遍历DFS
- DFS:695. Max Area of Island
- leetcode 695. Max Area of Island 解法
- [LeetCode] Algorithms-695. Max Area of Island
- Leetcode 695. Max Area of Island
- LeetCode-695. Max Area of Island
- LeetCode 695. Max Area of Island
- LeetCode-695. Max Area of Island
- [LeetCode] 695. Max Area of Island
- 【LeetCode】695. Max Area of Island
- LeetCode算法题——Max Area of Island
- LeetCode: Max Area of Island
- Max Area of Island(leetcode)
- leetCode-Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- 695. Max Area of Island
- c#学习笔记之单例
- 【Scikit-Learn 中文文档】预测目标 (y) 的转换
- Spark:Scala实现二次排序
- Java并发学习(十五)-并发工具Semaphore
- IntelliJ IDEA2017.3 激活
- leetcode 695. Max Area of Island——第一次成功的dfs
- 题目来源UVA
- android知识点链接(二)大多是网页的内容
- SLF4J: Class path contains multiple SLF4J bindings
- [jzoj]3479. 【NOIP2013模拟联考9】工作安排(work)
- 【Scikit-Learn 中文文档】数据集加载工具
- 设计模式-面向对象设计核心
- POJ-3669
- ssm开发使用redis作为缓存,使用步骤