*LeetCode 62. Unique Paths 记忆化搜索 or 组合数学
来源:互联网 发布:java中异步请求 编辑:程序博客网 时间:2024/06/05 20:45
https://leetcode.com/problems/unique-paths/
记忆化搜索的本质就是dp, 但是涉及图的时候 有时候思考会更顺一些
const int SIZE = 101;class Solution {public: int uniquePaths(int m, int n) { for(int i=0;i<m;i++) { for(int j=0;j<n;j++) dp[i][j] = -1; } return dfs(0,0, m, n); } int dfs(int hang, int lie, int m, int n) { if(dp[hang][lie] >= 0 )return dp[hang][lie]; if(hang == m-1 && lie == n-1) return (dp[m-1][n-1]=1); int ret = 0; if(hang+1 < m) ret += dfs(hang+1,lie, m, n); if(lie+1 < n) ret += dfs(hang, lie+1, m, n); return (dp[hang][lie]=ret); }private : int dp[SIZE][SIZE];};
const int SIZE = 101;class Solution {public: int uniquePaths(int m, int n) { for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(i == 0 && j == 0) dp[i][j] =1; else { dp[i][j] = 0; if(i-1 >=0) dp[i][j] += dp[i-1][j]; if(j-1>=0) dp[i][j] += dp[i][j-1]; } } return dp[m-1][n-1]; }private : int dp[SIZE][SIZE];};
组合数学的方法,高中数学题,其实正常来算也是O(n^2)使用
C(M-1,N-1)+C(M-1,N)=C(M,N)
也可以这样http://www.bubuko.com/infodetail-911131.html
0 0
- *LeetCode 62. Unique Paths 记忆化搜索 or 组合数学
- LeetCode 62 Unique Paths(记忆化搜索)
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- [Leetcode] 62. Unique Paths
- [leetcode] 62.Unique Paths
- [leetcode] 62.Unique Paths
- leetcode 62. Unique Paths
- Leetcode 62. Unique Paths
- [leetcode] 62. Unique Paths
- 62. Unique Paths LeetCode
- [LeetCode]62. Unique Paths
- 【LeetCOde】62. Unique Paths
- [LeetCode]62. Unique Paths
- leetcode 62. Unique Paths
- LeetCode 62. Unique Paths
- LeetCode *** 62. Unique Paths
- 遍历iOS相册
- openfire之SSL认证
- 【设计模式】中介者模式
- Java web项目的各种路径的获取
- ajax请求返回xml数据(含服务端代码)
- *LeetCode 62. Unique Paths 记忆化搜索 or 组合数学
- A 牛牛
- 信道的极限容量——奈氏准则、奈圭斯特公式、信噪比、香农公式
- 项目导包-->V7包报错及解决方法
- Java操作远程共享资源(JCIFS)
- 韩顺平 javascript教学视频_学习笔记8_js系统函数_js函数调用方式
- 字符串的序列化
- 网页设计和网页开发人员常用到的Google Chrome扩展
- 树莓派+DHT11温湿度传感器+yeelink物联网云