[LeetCode]题解(python):062-Unique Paths

题目来源:

  https://leetcode.com/problems/unique-paths/


题意分析:

  给定两个整型m,n。判断从一个m×n的矩阵里面,从(0,0)走到(m-1,n-1)一共有多少种法(只能往下和往右走)。


题目思路:

  这可以看成一个组合问题。从(0,0)到(m-1,n-1)一共要走m - 1次向下,n-1次向右。也就是在n + m - 2次中选出m-1次向下,也就是C(m + n - 2,m-1)。


代码(python):

  

 1 import math
 2 class Solution(object):
 3     def uniquePaths(self, m, n):
 4         """
 5         :type m: int
 6         :type n: int
 7         :rtype: int
 8         """
 9         ans = 1;tmp = 1;m -= 1;n -= 1
10         t = min(m,n)
11         i = 0
12         while i < t:
13             ans *= (m + n - i)
14             tmp *= (t - i)
15             i += 1
16         return ans /tmp
View Code

转载请注明出处:http://www.cnblogs.com/chruny/p/5008210.html

原文地址:https://www.cnblogs.com/chruny/p/5008210.html