【leetcode】324. 摆动排序 II

void wiggleSort(int* nums, int numsSize){
    int hash[5001]={0}, i, j=0;
    for(i=0; i<numsSize; i++)
        hash[nums[i]]++;
    
    i=(numsSize%2)?numsSize-1 :numsSize-2;
    for(; i>=0;){
        if(hash[j]){
            nums[i]=j;
            hash[j]--;
            i-=2;
        }
        else
            j++;
    }
    i=(numsSize%2)?numsSize-2 :numsSize-1;
    for(; i>=0;){
        if(hash[j]){
            nums[i]=j;
            hash[j]--;
            i-=2;
        }
        else
            j++;
    }
}

 

posted @ 2021-01-24 11:33  温暖了寂寞  阅读(53)  评论(0编辑  收藏  举报