首页 > 其他 > 详细

Unique Paths

时间:2015-07-13 11:40:12      阅读:246      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/unique-paths/

递归问题转换成动态规划问题。每个问题可以分解成p[m][n]=p[m-1][n]+p[m][n-1]

动态规划:做一个动态二维数组,用于存放每步的解。最终的循环就是矩阵的所有点遍历一遍。

时间复杂度为m*n的矩阵遍历。

 1 class Solution {
 2 public:
 3     int uniquePaths(int m, int n) {
 4         int ** p;
 5         p=new int *[m+1];
 6         for(int i=1;i<m+1;i++)
 7             p[i]=new int[n+1];
 8         
 9         for(int i=1;i<=m;i++)
10         {
11             for(int j=1;j<=n;j++)
12             {
13                 if(i==1||j==1)
14                     p[i][j]=1;
15                 else
16                     p[i][j]=p[i-1][j]+p[i][j-1];
17             }
18         }
19         return p[m][n];
20         
21     }
22 };

 

Unique Paths

原文:http://www.cnblogs.com/aguai1992/p/4642281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!