A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Ni,j=Ni−1,j+Ni,j−1
当 i=0 and j=0 时候 Ni,j=1
TLE代码
class Solution(object):
def uniquePaths(self, m, n):
if m==1 or n==1:
return 1
else:
return self.uniquePaths(m-1,n)+self.uniquePaths(m,n-1)
Accept代码
class Solution(object):
def uniquePaths(self, m, n):
num=[]
print num
for i in range(m):
num.append([])
for j in range(n):
if i==0 or j==0:
num[i].append(1)
else:
num[i].append(num[i][j-1]+num[i-1][j])
return num[m-1][n-1]