从左上角到右下角的过程中,我们需要移动m + n - 2次,其中有m - 1次向下移动,n - 1次向右移动。
因此路径的总数 =
C
(
m
+
n
−
2
,
m
−
1
)
C(m+n-2,m-1)
C(m+n−2,m−1)
AC代码
classSolution{publicintuniquePaths(int m,int n){long ans =1;for(int x = n, y =1; y < m;++x,++y)
ans = ans * x / y;return(int) ans;}}
本地测试代码
package com.company;publicclassSolution_62{publicstaticintuniquePaths(int m,int n){long ans =1;for(int x = n, y =1; y < m;++x,++y)
ans = ans * x / y;return(int) ans;}publicstaticvoidmain(String[] args){
System.out.println(uniquePaths(3,7));
System.out.println(uniquePaths(3,2));}}