leetcode-329. Longest Increasing Path in a Matrix
来源:互联网 发布:项目经理考试软件app 编辑:程序博客网 时间:2024/06/18 17:04
考察点:dfs,dp,memory;
思路:遍历每个节点,用dp存储该节点的最长值;dfs函数返回该节点的最长值,dfs里,如果已经存储,就直接返回,否则就四个方向依次遍历,如果比它小就放弃这条路;最后得出结果就返回最大的。
C++代码:
class Solution {public: vector<vector<int>> dirs={{0,1},{1,0},{0,-1},{-1,0}}; int longestIncreasingPath(vector<vector<int>>& matrix) { if (matrix.size()==0 || matrix[0].size()==0) return 0; int m = matrix.size(); int n = matrix[0].size(); int ret = 1; vector<vector<int>> dp(m, vector<int>(n, 0)); for (int i=0; i<m; i++) { for (int j=0; j<n; j++) { int temp = dfs(matrix,dp,i,j, m, n); ret = max(ret, temp); } } return ret; } int dfs(vector<vector<int>>& matrix, vector<vector<int>>& dp, int i, int j, int m, int n) { if (dp[i][j] != 0) return dp[i][j]; int max1 = 1; for (auto dir : dirs) { int x = i + dir[0]; int y = j + dir[1]; if (x<0 || x >=m || y<0 || y>=n || matrix[x][y] <= matrix[i][j]) continue; int len = 1 + dfs(matrix, dp, x, y, m , n); max1 = max(max1, len); } dp[i][j] = max1; return max1; }};
0 0
- leetcode Longest Increasing Path in a Matrix
- [LeetCode] Longest Increasing Path in a Matrix
- leetcode: Longest Increasing Path in a Matrix
- Leetcode: Longest Increasing Path in a Matrix
- leetcode:Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- [Leetcode]Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix | leetcode
- LeetCode Longest Increasing Path in a Matrix
- 【Leetcode】Longest Increasing Path in a Matrix
- LeetCode:Longest Increasing Path in a Matrix
- [leetcode]Longest Increasing Path in a Matrix
- Leetcode Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- Leetcode 329. Longest Increasing Path in a Matrix
- LeetCode—329. Longest Increasing Path in a Matrix
- [leetcode] - 329. Longest Increasing Path in a Matrix
- 329. Longest Increasing Path in a Matrix LeetCode
- BZOJ 4403: 序列统计 Lucas
- 8天学通MongoDB——第二天 细说增删查改
- Kmeans python 可视化
- thinkphp怎样获取控制器名称和操作名称(不是在模板里面获取)
- *指针_函数指针、指针控制数组、链表(结构体指针)
- leetcode-329. Longest Increasing Path in a Matrix
- 基于SpringBoot的微服务与“头文件”
- tomcat 远程调试配置
- MySql 外键
- highcharts绘制饼图html页面源码
- 解决easyUI跨域问题
- Redis集群搭建与简单使用
- 昨天没做出来的一道题目,不难,但是一直出现段错误
- Fade数字切换动效