leetcode 62. Unique Paths

原题:
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?
大意:
一个机器人从(1,1)走到(m,n)有多少种走法。=_=

class Solution
{
    int dp[101][101];
public:
    int uniquePaths(int m, int n)
    {
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=m;i++)
            dp[i][1]=1;
        for(int i=1;i<=n;i++)
            dp[1][i]=1;
        for(int i=2;i<=m;i++)
        {
            for(int j=2;j<=n;j++)
            {
                dp[i][j]+=(dp[i-1][j]+dp[i][j-1]);
            }
        }
        return dp[m][n];
    }

};

解答:
我发现我挑出来的题目都这么简单呢~~ 其实根据高中的排列组合公式就能直接求出来,C(m+n,n)就是答案。但是数目比较大,所以考虑用递推的方法。走到dp[i][j]的方法等于走到dp[i-1][j]与dp[i][j-1]的方法的和。 最后dp[m][n]就是答案

你可能感兴趣的:(LeetCode)