LeetCode 62. Unique Paths (Python)最优解--Apolo_Shane

#二维数组加上for循环
class Solution:
    def uniquePaths(self, m, n):

        l=[[1]*n for i in range(m)]#生成m行n列的二维数组
        for i in range(1,m):
            for j in range(1,n):
                l[i][j]=l[i-1][j]+l[i][j-1]
        return l[m-1][n-1]
#第二种方法,直接用数学中的组合公式来做
import math
class Solution:
    def uniquePaths(self, m, n):

        return int(math.factorial(m+n-2)/(math.factorial(m-1)*math.factorial(n-1)))

你可能感兴趣的:(LeetCode/OJ,算法Algorithm,Python)