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
相关推荐
luj_17681 小时前
中锋卡位与开放线博弈
c语言·开发语言·网络·经验分享·算法
洋不写bug1 小时前
二叉树(四)经典算法题目解析,公共祖先,二叉树构造,非递归遍历
数据库·算法·二叉树·二叉树构造·公共祖先
hansang_IR2 小时前
【题解】P4454 [CQOI2018] 破解D-H协议
c++·算法·密码学·数论
梓䈑2 小时前
【动态规划系列】路径问题、子数组 和 子序列问题、背包问题
c++·算法·动态规划
aqiu1111112 小时前
【数位 DP / 记忆化搜索】蓝桥云课 - 光轨区的 AI 数 题解
c++·算法·蓝桥杯·数位dp
地平线开发者2 小时前
Horizon J6m 部署 YOLOv8s INT8 精度下降问题排查记录
算法
橘子汽水1682 小时前
Leetcode 200,994岛屿数量,腐烂的橘子
数据结构·算法·leetcode
kiracrimson3 小时前
【无标题】
数据结构
hansang_IR4 小时前
【题解】P5364 [SNOI2017] 礼物
c++·线性代数·算法