Skip to content

Files

Latest commit

b5c378c · May 9, 2018

History

History
39 lines (23 loc) · 702 Bytes

062._Unique_Paths.md

File metadata and controls

39 lines (23 loc) · 702 Bytes

62. Unique Paths

题目:

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

难度:

Medium

思路:

dp 典型, 走到(i, j) 可能的方法是走到 (i-1,j) 加上走到 (i, j-1)之和。

同时针对于第一行和第一列都只能是一种走法。

以上这个都是走向只可能是向右或者向下限制的。

class Solution(object):
    def uniquePaths(self, m, n):
        """
        :type m: int
        :type n: int
        :rtype: int
        """
        dp = [[1 for _ in range(n)] for _ in range(m)]

        for i in range(1,m):
            for j in range(1, n):
                dp[i][j] = dp[i-1][j] + dp[i][j-1]

        return dp[m-1][n-1]