您的位置:首页 > 产品设计 > UI/UE

LeetCode Unique Paths

2015-04-08 20:45 323 查看
题目如下:

  

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?



Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

这题其实非常简单,放着就是为了描述动态规划算法。看了代码就明白了。

int uniquePaths(int m, int n) {
int *A;
A=(int*)malloc(sizeof(int)*m*n);
int a,b;
for(a=m-1,b=0;b<n;b++){
A[a*n+b]=1;
}
for(b=n-1,a=0;a<m;a++){
A[a*n+b]=1;
}
for(a=m-2;a>=0;a--){
for(b=n-2;b>=0;b--){
A[a*n+b]=A[(a+1)*n+b]+A[a*n+b+1];
}
}
return A[0];
}


类似的题Minimum Path Sum也可以通过这个方法解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: