Leetcode Unique Paths
来源:互联网 发布:应用包安装软件 编辑:程序博客网 时间:2024/05/17 07:33
用了两种方法。
第一种是直接计算组合数,C(m-1, m+n-2),本来以为会溢出,m, n最大能到100,结果测试数据还是过了。
第二种是dp,dp[i][j] = dp[i-1][j]+dp[i][j-1,dp[i][j]表示到达坐标为(i,j)方格的方案数。
Solution 1:
class Solution {public: int uniquePaths(int m, int n) { // Note: The Solution object is instantiated only once and is reused by each test case. int s = m+n-2, r = min(m-1, n-1); long long ans = 1; for(int i = 0, j = 1; i < r; i++){ ans *= (s-i); for(; j <= r && ans % j == 0; j++) ans /= j; } return ans; }};
Solution 2:
class Solution {public: int uniquePaths(int m, int n) { // Note: The Solution object is instantiated only once and is reused by each test case. vector<vector<int> > dp(m, vector<int>(n, 1)); for(int i = 1; i < m; i++){ for(int j = 1; j < n; j++){ dp[i][j] = dp[i-1][j]+dp[i][j-1]; } } return dp[m-1][n-1]; }};
- leetcode Unique Paths && Unique Paths
- Leetcode-Unique Paths&Unique PathsII
- [LeetCode] Unique Paths and Unique Paths II
- leetcode--Unique Paths && Unique Paths ii
- leetcode--Unique Paths && Unique Paths ii
- 【Leetcode】【python】Unique Paths/Unique Paths II
- LeetCode: Unique Paths
- LeetCode: Unique Paths II
- [Leetcode] Unique Paths
- [Leetcode] Unique Paths II
- LeetCode : Unique Paths
- LeetCode : Unique Paths II
- leetcode 9: Unique Paths
- [LeetCode] Unique Paths
- [LeetCode] Unique Paths II
- [LeetCode]Unique Paths
- [LeetCode]Unique Paths II
- [LeetCode] Unique Paths
- 遗憾是青春的纪念册
- 用异或交换两个整数的陷阱
- Oracle 12c GoldenGate View
- Irrlicht学习备忘录——8 SpecialFX
- UML快速入门
- Leetcode Unique Paths
- C++_构造函数与析构函数
- 2014年10月26完美世界校招java中的两道java题
- Login failed for user 'IIS APPPOOL\DefaultAppPool'
- Spring MVC中redirect与forward
- 安装MangOS
- ER模型转关系模型
- webservice之CXF注解实现(三)
- Android 读写xml文件