62. 不同路径

  1. 题目链接

  2. 解题思路:数学题,一共要走m + n - 2步,选择其中的n - 1往右走,或者选择其中的m - 1往下走,就是C(n-1)/(n+m-2)

  3. 代码

    class Solution:
        def uniquePaths(self, m: int, n: int) -> int:
            # 计算C(m - 1) / m + n - 2
            total = m + n - 2
            need = min(m, n) - 1
            ans = 1
            for i in range(1, need + 1, 1):
                ans *= total - i + 1
                ans /= i
            return int(ans)
    
    
posted @ 2024-12-22 19:41  ouyangxx  阅读(8)  评论(0)    收藏  举报