我想解决这个问题已经有一段时间了。给出了一个M×N网格,我们要找到从左上角到右下角的路径数。在
我用Java解决了这个问题,并用Python编写了另一个解决方案。现在我想用记忆表修改前面的解决方案,以便在右下角的单元格中收集最终答案。单元格的值是其左右相邻单元格的总和。在
{1美元^
这是密码我不能调试:-在class Solution:
#Actual Recursive function
def paths(self,row,col):
if row == 0 or col == 0:
self.Mat[row][col] = 1
return 1
self.Mat[row][col-1] = self.paths(row, col-1)
self.Mat[row-1][col] = self.paths(row-1, col)
self.Mat[row][col] = self.Mat[row][col-1] + self.Mat[row-1][col]
return self.Mat[row][col]
# Driver Function. This will be called
def uniquePaths(self, A, B):
self.Mat = [[-1]*B]*A
ans = self.paths(A-1, B-1)
return self.Mat[A-1][B-1]
这是我以前的解决方案,它是有效的,但不使用记忆表。在
^{pr2}$
试验案例:-
3,3=6
15,9=319770