leetcode62.[DP] Unique Paths

来源:互联网 发布:微信淘宝客封号 编辑:程序博客网 时间:2024/06/03 01:26

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=Ni1,j+Ni,j1

i=0andj=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]
0 0
原创粉丝点击