62. Unique Paths
来源:互联网 发布:远程桌面控制软件 编辑:程序博客网 时间:2024/05/27 09:45
先给出两种超时的方法。
//23 12class Solution {public: int uniquePaths(int m, int n) { if(m==1||n==1) return 1; int sum=0; for(int i=1;i<=m;i++) { sum+=uniquePaths(i,n-1); } return sum; }};
//51 9class Solution {public: int uniquePaths(int m, int n) { if(m==1||n==1) return 1; if(m==2) return n; if(n==2) return m; return uniquePaths(m,n-1)+uniquePaths(m-1,n); }};
想了想发现,自顶向下的方法行不通,那就只能是自底向上了。
//OK,ac了 思路很清晰,没什么问题class Solution {public: int uniquePaths(int m, int n) { int map[100][100]={0}; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { if(i==1) map[i-1][j-1]=1; else if(i==2) map[i-1][j-1]=j; else map[i-1][j-1]=map[i-2][j-1]+map[i-1][j-2]; } return map[m-1][n-1]; }};
0 0
- 62. Unique Paths && 63. Unique Paths II
- 62. Unique Paths \ 63. Unique Paths II
- [LeetCode]62.Unique Paths
- LeetCode --- 62. Unique Paths
- LeetCode 62.Unique Paths
- [Leetcode] 62. Unique Paths
- [leetcode] 62.Unique Paths
- 62. Unique Paths
- [leetcode] 62.Unique Paths
- leetcode 62. Unique Paths
- Leetcode 62. Unique Paths
- [leetcode] 62. Unique Paths
- 62. Unique Paths LeetCode
- 62. Unique Paths
- 62. Unique Paths
- [LeetCode]62. Unique Paths
- 【LeetCOde】62. Unique Paths
- [LeetCode]62. Unique Paths
- JAVA基础总结(1)
- Swift2中的标准函数
- 第58课:使用Java和Scala在IDE中开发DataFrame实战学习笔记
- 数据库的基本操作
- 你真的会写单例模式吗——Java实现
- 62. Unique Paths
- POJ1703 Find them, Catch them(并查集)
- Uva299——Train Swapping
- onWindowFoucusedChanged
- JVMGC——垃圾收集器的分类
- DRP之旅(2)——当ASP遇到JSP
- Lintcode 合并两个排序的链表
- leetcode 77. Combinations
- CodeForces 332B Maximum Absurdity