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?
意思很明确,只能往下往右,从左上角到右下角,有多少条路?
第一个思路:动态规划、
到达某一格的路径数目=到达它上一格的路径数目+达到它左侧一格的路径数目
由于只能往下或者往右,故第一行和第一列的所有格子都只有一条路径。
class Solution:
# @return an integer
def uniquePaths(self, m, n):
if m == 1 and n == 1:
return 1
feat = [[1] * n for row in range(m)]
feat[0][0] = 0
for i in range(1,n): #第一行第一列都是1
for j in range(1,m):
feat[j][i] = feat[j-1][i] + feat[j][i-1]
return feat[m-1][n-1]
第二个思路:用0,1分别表示往下,往右,对于m*n的格子,结果就是有多少种m-1个0和n-1个1的排列方式。