2026.9.9

994 腐烂的桃子

python 复制代码
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        m,n=len(grid),len(grid[0])
        fresh=0
        q=[]
        ans=0
        for i,row in enumerate(grid):
            for j,x in enumerate(row):
                if x==1:fresh+=1
                elif x==2:q.append((i,j))
        while q and fresh:
            tmp=q
            q=[]
            ans+=1
            for x,y in tmp:
                for i,j in (x-1,y),(x+1,y),(x,y-1),(x,y+1):
                    if 0<=i<m and 0<=j<n and grid[i][j]==1:
                        fresh-=1
                        q.append((i,j))
                        grid[i][j]=2
        return -1 if fresh else ans

46 全排列

python 复制代码
class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        n=len(nums)
        path=[0]*n
        on_path=[False]*n
        ans=[]

        def dfs(i:int)->None:
            if i==n:
                ans.append(path.copy())
                return
            for j,on in enumerate(on_path):
                if not on:
                    path[i]=nums[j]
                    on_path[j]=True
                    dfs(i+1)
                    on_path[j]=False
        
        dfs(0)
        return ans

78 子集

python 复制代码
class Solution:
    def subsets(self, nums: List[int]) -> List[List[int]]:
        n=len(nums)
        ans=[]
        path=[]

        def dfs(i:int)->None:
            if i==n:
                ans.append(path.copy())
                return
            dfs(i+1)
            path.append(nums[i])
            dfs(i+1)
            path.pop()
            
        dfs(0)
        return ans
相关推荐
武子康12 分钟前
驾驶轨迹换个坐标,就能给移动机器人用吗?
人工智能·算法·机器学习
David猪大卫26 分钟前
【C++修炼】map 和 set 的使用
c语言·开发语言·数据结构·c++·经验分享·笔记·学习
vvilkim2 小时前
数据结构类型、区别与应用场景:一篇讲透选型逻辑
数据结构
文丑颜不良啊3 小时前
LeetCode2461~2480题目解析
java·leetcode
文丑颜不良啊4 小时前
LeetCode2441~2460题目解析
java·leetcode
文丑颜不良啊5 小时前
LeetCode2521~2540题目解析
java·leetcode
lisw056 小时前
Dayhoff的PAM替换矩阵理论评介!
线性代数·算法·矩阵
阿卡好可爱7 小时前
Lua 热更新不生效:改完文件线上仍是旧逻辑,package.loaded 与 upvalue 重绑定详解
数据结构·分布式·后端·架构·lua
一起学安全技术7 小时前
拆大模型 03|注意力的一句话版本:不用论文讲清 Attention
深度学习·算法·语言模型·transformer
Delightrs8 小时前
Rust LeetCode238 算法题解
开发语言·算法·rust