[LeetCode] 62. Unique Paths
来源:互联网 发布:python生成一组随机数 编辑:程序博客网 时间:2024/06/06 04:39
思路:
全都是动态规划, 第一个方法就是开辟一块m * n的数组, 并都初始化为1, 遍历除第一行第一列以外的所有元素, 每个格子等于自己加上他上方的加上他左方的和.
第二个是优化一下只需要O(min(m, n))的空间.
int uniquePaths(int m, int n) { int count[m][n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) count[i][j] = 1; for (int i = 1; i < m; i++) for (int j = 1; j < n; j++) count[i][j] = count[i - 1][j] + count[i][j - 1]; return count[m - 1][n - 1];}
int uniquePaths(int m, int n) { if (m > n) return uniquePaths(n, m); int count[m]; for (int i = 0; i < m; i++) count[i] = 1; for (int j = 1; j < n; j++) for (int i = 1; i < m; i++) count[i] += count[i - 1]; return count[m - 1];}
0 0
- [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
- leetcode 62. Unique Paths
- Leetcode:62. Unique Paths
- qsort()函数
- jsp <%%> <%!%>的区别
- Android 自定义View 图片按Path运动和旋转
- 基本数据类型
- CSS3技巧:利用css3径向渐变做一张优惠券
- [LeetCode] 62. Unique Paths
- 获取分页的页码导航栏
- xUtils的网络请求简单封装
- 【HDU 1203】 I NEED A OFFER!(01背包)
- Codeforces 427C - Checkposts 极大连通分量Tarjan算法模板题(STL实现)
- iOS开发--下载安装cocoapods的详细教程、cocoapods使用第三方库
- Android TextView限制字数属性
- 错误:MySQL多列identity约束(VS2012)
- TraceView