方法
方法1:动态规划 O(mn)
1 | class Solution(object): |
仅仅改变grid自身1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20class Solution(object):
def minPathSum(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid:
return
r,c=len(grid),len(grid[0])
for i in range(1,r):
grid[i][0]=grid[i-1][0]+grid[i][0]
for i in range(1,c):
grid[0][i]=grid[0][i-1]+grid[0][i]
for i in range(1,r):
for j in range(1,c):
grid[i][j]=min(grid[i][j-1],grid[i-1][j])+grid[i][j]
return grid[-1][-1]
1 | class Solution(object): |