542. 01 Matrix刷题笔记

本文分享了一种改进的BFS算法,通过合并条件判断和使用collections.deque优化Python代码,以解决更新矩阵问题的超时问题。作者详细讲解了从0开始搜索、队列操作优化等关键步骤,提升代码执行效率。
摘要由CSDN通过智能技术生成

用bfs
pycharm代码一稿

class Solution:
    def updateMatrix(self, mat):
        m = len(mat)
        n = len(mat[0])
        directions = [(-1,0),(1,0),(0,-1),(0,1)]

        to_check2 = []

        for i in range(m):
            for j in range(n):
                if mat[i][j] == 0:
                    continue
                to_check1=[[i,j]]
                checked=[]
                sucess = 0

                while not sucess:
                    while to_check1:
                        [x,y] = to_check1.pop(0)
                        if 0<=x<m and 0<=y<n and [x,y] not in checked:
                            if mat[x][y]==0:
                                sucess = 1
                                mat[i][j] = abs(i-x)+abs(j-y)
                                break
                            else:
                                to_check2+=[[x-1,y],[x+1,y],[x,y-1],[x,y+1]]
                            checked += [[x, y]]

                    to_check1=to_check2
                    to_check2=[]
        return mat

mat = [[0,0,0],[0,1,0],[0,0,0]]
print(Solution().updateMatrix(mat))

mat = [[0,0,0],[0,1,0],[1,1,1]]
print(Solution().updateMatrix(mat))

运行超时,原因是从1开始搜索,改成从0搜索就好了,参考了这个大神的博客

        m = len(mat)
        n = len(mat[0])
        directions = [(-1,0),(1,0),(0,-1),(0,1)]
        q = []
        for i in range(m):
            for j in range(n):
                if mat[i][j] == 0:
                    q += [[i,j]]
                else:
                    mat[i][j] = 20000
        while q:
            [ix,iy] = q.pop(0)
            for dx,dy in directions:
                nx = ix+dx
                ny = iy+dy
                if 0<=nx<m and 0<=ny<n:
                    if mat[nx][ny] > mat[ix][iy]+1:
                        mat[nx][ny] = mat[ix][iy]+1
                        q += [[nx,ny]]
        return mat

运行结果
在这里插入图片描述
但是运行结果还是比较慢,究其原因,第一个是两个if可以合并,第二个是列表可以改成collections.deque(),因为deque是双端队列,双端队列的append()和pop()的时间复杂度为O(1),而list的insert(0,value),append以及pop()的时间复杂度为O(n)。
最终代码

import collections
class Solution:
    def updateMatrix(self, mat):
        m = len(mat)
        n = len(mat[0])
        directions = [(-1,0),(1,0),(0,-1),(0,1)]
        q = collections.deque()
        for i in range(m):
            for j in range(n):
                if mat[i][j] == 0:
                    q.append((i,j))
                else:
                    mat[i][j] = 20000
        while q:
            ix,iy = q.popleft()
            for dx,dy in directions:
                nx = ix+dx
                ny = iy+dy
                if 0<=nx<m and 0<=ny<n and mat[nx][ny] > mat[ix][iy]+1:
                        mat[nx][ny] = mat[ix][iy]+1
                        q.append((nx,ny))
        return mat

mat = [[0,0,0],[0,1,0],[0,0,0]]
print(Solution().updateMatrix(mat))

mat = [[0,0,0],[0,1,0],[1,1,1]]
print(Solution().updateMatrix(mat))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值