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
相关推荐
iiiiii1118 小时前
从最大熵到 SAC:Soft Q-Learning 与 Soft Actor-Critic 公式推导笔记
人工智能·笔记·算法·机器学习·强化学习
田里的水稻19 小时前
FA_排序算法-冒泡排序
java·算法·排序算法
Go-higher21 小时前
C语言--数据结构-栈、队列、堆
c语言·数据结构
AAA修煤气灶刘哥21 小时前
AI 写的 SQL 跑通了,为什么和财务报表对不上?
人工智能·后端·算法
IvanCodes1 天前
模型如何自我改进?拆解 RSI 的模型权重与 Harness 两条路线
人工智能·算法·ai·论文·agent·rsi
罗西的思考1 天前
【轻量化复刻大模型 / KIMI K3】MXFP4 / MXFP8 量化感知训练(QAT)& 演示
人工智能·算法·机器学习
郝学胜-神的一滴2 天前
Effective STL 条款 14:用扩容函数避免重新分配
linux·开发语言·网络·数据结构·c++·单片机
旖旎夜光2 天前
LeetCode 493: 翻转对 (归并排序) —— 题解
c++·笔记·学习·算法·leetcode·排序算法
阿enna2 天前
检测指标入门:IoU、Precision、Recall与mAP
人工智能·深度学习·算法·yolo·目标检测·计算机视觉·cnn