LEETCODE: Unique Paths

来源:互联网 发布:nsp网络 编辑:程序博客网 时间:2024/06/13 05:12

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?

幸亏long long 可以搞定,不过数量更多之后,还是需要其他办法。



class Solution {public:    int uniquePaths(int m, int n) {        if(m == 1 || n == 1) return 1;                int down = m + n - 2;        int up = m < n ? m - 1 : n -1;        long long totaldown = 1;        long long totalup = 1;        for(int ii = up; ii > 0; ii --) {            totaldown *= down;            totalup *= up;            down --;            up --;        }                return totaldown / totalup;    }};




0 0
原创粉丝点击