classSolution:deffindUnsortedSubarray(self, nums: List[int])->int:
l,r =0,len(nums)-1while l <= r:
Max =max(nums[l:r+1])
Min =min(nums[l:r+1])if nums[l]== Min:
l +=1elif nums[r]== Max:
r -=1else:breakreturnlen(nums[l:r+1])
classSolution:defsortArrayByParity(self, A: List[int])-> List[int]:
l =0
r =len(A)-1while l < r:# 左指针为奇数,右指针为偶数则交换if A[l]%2!=0and A[r]%2==0:
A[l],A[r]= A[r],A[l]
l +=1
r -=1# 左右都为偶数,则左移左指针elif A[r]%2==0and A[l]%2==0:
l +=1# 左右都为奇数,则右移右指针elif A[l]%2!=0and A[r]%2!=0:
r -=1# 左指针为偶数,右指针为奇数else:
l +=1
r -=1return A
classSolution:defsortArrayByParityII(self, A: List[int])-> List[int]:
odd =[]
even =[]
res =[]
n =len(A)for num in A:if num%2==0:
even.append(num)else:
odd.append(num)while n:
res.append(even.pop())
res.append(odd.pop())
n -=2return res