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