mycode time limited
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if not m or not n :
return 0
if m == 1 or n == 1:
return 1
return self.uniquePaths(m-1,n) + self.uniquePaths(m,n-1)
参考:
思路: 类似于金字塔那个题,考虑上一步即可
class Solution(object):
def uniquePaths(self, m, n):
"""
:type m: int
:type n: int
:rtype: int
"""
if m == 1 and n == 1:
list = [[1]]
elif m == 1 and n > 1:
list = [[1 for i in range(n)]]
elif m > 1 and n == 1:
list = [[1] for i in range(m)]
else:
list = [[0 for i in range(n)] for i in range(m)]
for i in range(0, n):
list[0][i] = 1
for i in range(0, m):
list[i][0] = 1
for i in range(1, m):
for j in range(1, n):
list[i][j] = list[i-1][j] + list[i][j-1]
return list[m-1][n-1]