LeetCode-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?

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

Note: m and n will be at most 100.

需要走m+n-2格 (减去起点),其中n-1格向下,总共C(m+n-2,n-1)种

感谢extended

class Solution {
public:
    
    int uniquePaths(int m, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        long long a=m-1,b=n-1;
        long long ret=1;
        for(long long i=1,j=2;i<=b;i++){
            ret*=a+i;
            while(j<=b&&ret%j==0){
                ret/=j;
                j++;
            }
        }
        return ret;
    }
};

 

posted @ 2013-08-19 22:41  懒猫欣  阅读(175)  评论(0编辑  收藏  举报