description:
https://leetcode.com/problems/unique-paths/
机器人从一堆方格的左上角走到右下角,只能往右或者往下走 ,问有几种走法
Note:
Example:
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right
Example 2:
Input: m = 7, n = 3
Output: 28
answer:
class Solution {
public:
int uniquePaths(int m, int n) {
double sum = 1, up = 1; // 这里一定是 double
int mi = m < n ? m : n; // 必须先找出最小的,虽然数学上两个排列相等,但是程序里因为数的范围限制神马的不清楚,会超时
for (int i = 0; i < mi - 1; i++) {
sum *= m + n - 2 - i;
up *= i + 1;
}
return (int)(sum / up); // 最后要把double 变到 integer
}
};
relative point get√:
hint :
就是在总步数 m + n - 2 里选 n-1 步 (m - 1)是往下走(右)