您的位置:首页 > 产品设计 > UI/UE

leetcode62.[DP] Unique Paths

2016-03-22 22:44 375 查看
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−1N_{i,j}=N_{i-1,j}+N_{i,j-1}

当i=0i=0andj=0j=0时候Ni,j=1N_{i,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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: