题一:62. 不同路径
链接
题目链接: 视频链接: 文章链接:
视频总结
编程思路
Me:
二维数组定义
力扣实战
思路一:dp
class Solution :
def uniquePaths ( self, m: int , n: int ) - > int :
dp = [ [ 1 for i in range ( n) ] for j in range ( m) ]
for i in range ( 1 , m) :
for j in range ( 1 , n) :
dp[ i] [ j] = dp[ i- 1 ] [ j] + dp[ i] [ j- 1 ]
return dp[ - 1 ] [ - 1 ]
class Solution :
def uniquePaths ( self, m: int , n: int ) - > int :
dp = [ 1 ] * n
for i in range ( 1 , m) :
for j in range ( 1 , n) :
dp[ j] = dp[ j] + dp[ j- 1 ]
return dp[ - 1 ]
思路二:dfs
class Solution :
def uniquePaths ( self, m: int , n: int ) - > int :
def func ( i, j, m, n) :
if i> m or j> n:
return 0
if i== m and j== n:
return 1
down = func( i+ 1 , j, m, n)
right = func( i, j+ 1 , m, n)
return down + right
res= func( 1 , 1 , m, n)
return res
题二:63. 不同路径 II
链接
题目链接: 视频链接: 文章链接:
视频总结
关键点
初始化不一样,行列有一个为障碍,其他的都得为0
编程思路
力扣实战
思路一:
class Solution :
def uniquePathsWithObstacles ( self, obstacleGrid: List[ List[ int ] ] ) - > int :
m = len ( obstacleGrid)
n = len ( obstacleGrid[ 0 ] )
if n== 1 and m== 1 :
return ( 1 - obstacleGrid[ 0 ] [ 0 ] )
dp = [ 0 ] * n
for j in range ( 0 , n) :
if obstacleGrid[ 0 ] [ j] != 1 :
dp[ j] = 1
else :
break
for i in range ( 1 , m) :
if obstacleGrid[ i] [ 0 ] == 1 :
dp[ 0 ] = 0
for j in range ( 1 , n) :
if obstacleGrid[ i] [ j] == 1 :
dp[ j] = 0
else :
dp[ j] = dp[ j] + dp[ j- 1 ]
return dp[ - 1 ]
class Solution :
def uniquePathsWithObstacles ( self, obstacleGrid: List[ List[ int ] ] ) - > int :
row = len ( obstacleGrid)
col = len ( obstacleGrid[ 0 ] )
dp = [ [ 0 for _ in range ( col) ] for _ in range ( row) ]
dp[ 0 ] [ 0 ] = 0 if obstacleGrid[ 0 ] [ 0 ] == 1 else 1
if dp[ 0 ] [ 0 ] == 0 :
return 0
for i in range ( 1 , col) :
if obstacleGrid[ 0 ] [ i] == 1 :
break
dp[ 0 ] [ i] = 1
for i in range ( 1 , row) :
if obstacleGrid[ i] [ 0 ] == 1 :
break
dp[ i] [ 0 ] = 1
for i in range ( 1 , row) :
for j in range ( 1 , col) :
if obstacleGrid[ i] [ j] == 0 :
dp[ i] [ j] = dp[ i - 1 ] [ j] + dp[ i] [ j - 1 ]
return dp[ - 1 ] [ - 1 ]
思路二:
class Solution :
def uniquePathsWithObstacles ( self, obstacleGrid: List[ List[ int ] ] ) - > int :
m = len ( obstacleGrid)
n = len ( obstacleGrid[ 0 ] )
dp= [ [ 0 for i in range ( n) ] for j in range ( m) ]
for i in range ( n) :
if obstacleGrid[ 0 ] [ i] == 0 :
dp[ 0 ] [ i] = 1
else :
break
for j in range ( m) :
if obstacleGrid[ j] [ 0 ] == 0 :
dp[ j] [ 0 ] = 1
else :
break
for i in range ( 1 , m) :
for j in range ( 1 , n) :
if obstacleGrid[ i] [ j] == 1 :
dp[ i] [ j] = 0
else :
dp[ i] [ j] = dp[ i- 1 ] [ j] + dp[ i] [ j- 1 ]
return dp[ - 1 ] [ - 1 ]
class Solution :
"""
使用一维dp数组
"""
def uniquePathsWithObstacles ( self, obstacleGrid: List[ List[ int ] ] ) - > int :
m, n = len ( obstacleGrid) , len ( obstacleGrid[ 0 ] )
curr = [ 0 ] * n
for j in range ( n) :
if obstacleGrid[ 0 ] [ j] == 1 :
break
curr[ j] = 1
for i in range ( 1 , m) :
for j in range ( n) :
if obstacleGrid[ i] [ j] == 1 :
curr[ j] = 0
elif j > 0 :
curr[ j] = curr[ j] + curr[ j - 1 ]
return curr[ n - 1 ]
文档总结
1. 不同的写法,可学习