6 lines C++ using sort


  • 0
        void wiggleSort(vector<int>& nums) {
            int n = nums.size();
            vector<int>sorted(nums);
            sort(sorted.begin(), sorted.end());
            int i(0), j((n+1)/2);
            for(int k = n-1; k >= 0; k--)
                nums[k] = k % 2 == 0 ? sorted[i++] : sorted[j++];
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.