Leetcode Unique Paths
来源:互联网 发布:泳衣淘宝网 编辑:程序博客网 时间:2024/06/02 02:06
题意:走棋盘,从左上到右下一共有多少种方法。
思路:求组合数。
class Solution {public: int uniquePaths(int m, int n) { int a = m - 1; int b = n - 1; return cc(min(a, b), b + a); } int cc(int u, int d) { if(u == 0) return 1; if(u == 1) return d; int re = 1; vector<bool> mark(u, true); for(int i = d; i > d - u; i --) { re *= i; for(int j = 0; j < mark.size(); j++) { if(re % (j + 1) == 0 && mark[j]) { re /= j + 1; mark[j] = false; } } } return re; }};
另一种思路是用递推。
class Solution {public: int uniquePaths(int m, int n) { vector<vector<int>> t(m, vector<int> (n, 1)); for(int i = 1; i < m ; ++ i) { for(int j = 1; j < n; j ++) { t[i][j] = t[i - 1][j] + t[i][j - 1]; } } return t[m - 1][n - 1]; }};
0 0
- 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
- Intent中addFlags()和SetFlags()的区别和详解
- knockout + require + director 构建单页面程序(knockout)
- HELLO, WORLD
- latex学习笔记之一(工具篇)
- mysql给root开启远程访问权限,修改root密码
- Leetcode Unique Paths
- ubuntu搭建gitlab服务器
- 手机安装burpsuit证书截取https请求
- 云计算复习笔记一
- 阿里云直播服务 sdk demo php
- 安装chardet 问题
- mysql 5.7 版本后数据没有字段 password 所以修改 root 密码 需要
- PostgreSQL表空间创建、删除
- Leetcode 85. Maximal Rectangle (Hard) (cpp)