LEETCODE-DAY39


title: LEETCODE-DAY39

date: 2024-03-30 17:24:46
tags:

今日内容:62.不同路径、63. 不同路径 II

T1

python 复制代码
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp=[[0 for _ in range(n+1)]for _ in range(m+1)]
        dp[1][1]=1
        for i in range(1,m+1):
            for j in range(1,n+1):
                dp[i][j]=dp[i-1][j]+dp[i][j-1]

        return dp[m][n]

输入

m =

3

n =

2

输出

0

预期结果

3

dp[1][1]被覆盖了

python 复制代码
class Solution:
    def uniquePaths(self, m: int, n: int) -> int:
        dp=[[0 for _ in range(n+1)]for _ in range(m+1)]
        dp[1][1]=1
        for i in range(1,m+1):
            for j in range(1,n+1):
                if i==1 and j==1:
                    continue
                dp[i][j]=dp[i-1][j]+dp[i][j-1]

        return dp[m][n]

AC

T2

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        for i in range(m):
            dp[i][0]=1
        for j in range(n):
            dp[0][j]=1

        for i in range(1,m):
            for j in range(1,n):
      
                dp[i][j]=dp[i-1][j]+dp[i][j-1]
        res=dp[m-1][n-1]
        for i in range(m):
            for j in range(n):
                if obstacleGrid[i][j]==1:
                    res-=dp[i][j]*dp[m-i-1][n-j-1]
                    
        return res

输入

obstacleGrid =

\[1,1\]

输出

-1

预期结果

0

python 复制代码
#同上
return max(res,0)

输入

obstacleGrid =

\[0,0,0,0\],\[0,1,0,0\],\[0,0,0,0\],\[0,0,1,0\],\[0,0,0,0\]

输出

0

预期结果

7

需要换一种思路

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        if obstacleGrid[0][0]==1:
            return 0
        for i in range(m):
            if obstacleGrid[i][0]==1:
                dp[i][0]=0
            else:
                dp[i][0]=1
        for j in range(n):
            if obstacleGrid[0][j]==1:
                dp[0][j]=0
            else:
                dp[0][j]=1


        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[m-1][n-1]

输入

obstacleGrid =

\[0,0\],\[1,1\],\[0,0\]

输出

1

预期结果

0

初始化有问题

第一行遇到障碍后障碍之后的列也应初始化为0

python 复制代码
class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m=len(obstacleGrid)
        n=len(obstacleGrid[0])
        dp=[[0 for _ in range(n)]for _ in range(m)]
        if obstacleGrid[0][0]==1:
            return 0
        for i in range(m):
            if obstacleGrid[i][0]==1:
                dp[i][0]=0
            elif i>0 and dp[i-1][0]==0:
                dp[i][0]=0
            else:
                dp[i][0]=1
        for j in range(n):
            if obstacleGrid[0][j]==1:
                dp[0][j]=0
            elif j>0 and dp[0][j-1]==0:
                dp[0][j]=0
            else:
                dp[0][j]=1


        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[m-1][n-1]

AC

python 复制代码
python 复制代码
相关推荐
玩电脑的辣条哥2 分钟前
一台服务器已经有个python3.11版本了,如何手动安装 Python 3.10,两个版本共存
服务器·python·python3.11
weixin_307779138 分钟前
PySpark实现ABC_manage_channel逻辑
开发语言·python·spark
莹莹学编程—成长记1 小时前
string的模拟实现
服务器·c++·算法
海天一色y1 小时前
Pycharm(十六)面向对象进阶
ide·python·pycharm
??? Meggie1 小时前
【Python】保持Selenium稳定爬取的方法(防检测策略)
开发语言·python·selenium
XIE3922 小时前
Browser-use使用教程
python
酷爱码3 小时前
如何通过python连接hive,并对里面的表进行增删改查操作
开发语言·hive·python
蹦蹦跳跳真可爱5893 小时前
Python----深度学习(基于深度学习Pytroch簇分类,圆环分类,月牙分类)
人工智能·pytorch·python·深度学习·分类
ShiinaMashirol6 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
MinggeQingchun6 小时前
Python - 爬虫-网页解析数据-库lxml(支持XPath)
爬虫·python·xpath·lxml