classSolution:defwiggleSort(self,nums:List[int])->None:""" Do not return anything, modify nums in-place instead. """arr=sorted(nums)n=len(arr)i,j=(n-1)>>1,n-1forkinrange(n):ifk%2==0:nums[k]=arr[i]i-=1else:nums[k]=arr[j]j-=1
/** * @param {number[]} nums * @return {void} Do not return anything, modify nums in-place instead. */varwiggleSort=function(nums){letbucket=newArray(5001).fill(0);for(constvofnums){bucket[v]++;}constn=nums.length;letj=5000;for(leti=1;i<n;i+=2){while(bucket[j]==0){--j;}nums[i]=j;--bucket[j];}for(leti=0;i<n;i+=2){while(bucket[j]==0){--j;}nums[i]=j;--bucket[j];}};
方法二:桶排序
1 2 3 4 5 6 7 8 91011121314151617181920
classSolution:defwiggleSort(self,nums:List[int])->None:""" Do not return anything, modify nums in-place instead. """bucket=[0]*5001forvinnums:bucket[v]+=1n=len(nums)j=5000foriinrange(1,n,2):whilebucket[j]==0:j-=1nums[i]=jbucket[j]-=1foriinrange(0,n,2):whilebucket[j]==0:j-=1nums[i]=jbucket[j]-=1