2026.9.17

75 颜色分类

python 复制代码
class Solution:
    def sortColors(self, nums: list[int]) -> None:
        p0=p1=0
        for i,x in enumerate(nums):
            nums[i]=2
            if x<=1:
                nums[p1]=1
                p1+=1
            if x==0:
                nums[p0]=0
                p0+=1

31 下一个排列

python 复制代码
class Solution:
    def nextPermutation(self, nums: list[int]) -> None:
        n=len(nums)
        i=n-2
        while nums[i]>=nums[i+1] and i>=0:
            i-=1
        if i>=0:
            j=n-1
            while nums[j]<=nums[i]:
                j-=1
            nums[i],nums[j]=nums[j],nums[i]
        left=i+1
        right=n-1
        while left<right:
            nums[left],nums[right]=nums[right],nums[left]
            left+=1
            right-=1

287 寻找重复数

python 复制代码
class Solution:
    def findDuplicate(self, nums: list[int]) -> int:
        slow=fast=0
        while True:
            slow=nums[slow]
            fast=nums[nums[fast]]
            if slow==fast:break
        head=0
        while head!=slow:
            head=nums[head]
            slow=nums[slow]
        return head
相关推荐
文丑颜不良啊10 分钟前
LeetCode3401~3420题目解析
java·leetcode
AvocadoZeroOneOI1 小时前
B4500 [GESP202603 三级] 凯撒密码 详细解题报告
c++·算法
文丑颜不良啊1 小时前
LeetCode3421~3440题目解析
java·leetcode
cjz38993 小时前
双指针技巧:覆盖式双指针(原地覆盖)
算法
console.log('npc')5 小时前
Python 学习教程 · 第4课:数据结构
数据结构·python·学习
橘子136 小时前
【高级数据库】关系代数基本概念
算法
电科_银尘17 小时前
【大模型算法与工程 15】手写Self-Attention:Q、K、V与缩放点积
算法
郝学胜-神的一滴21 小时前
Effective STL 条款03:容器中的对象复制要廉价且正确
开发语言·数据结构·c++·算法·rpc·stl
秋天的怀念_David21 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
LK_071 天前
【力扣】142. 环形链表 II |Python
python·leetcode·链表