Simple C++ Solution O(nlogn)


  • -1
    S

    The idea is to sort the array normally first, then insert the last half of the array into certain positions respectively.

    class Solution
    {
    public:
    void wiggleSort(vector<int>& nums) {

        if(nums.size()<3)return;
       sort(nums.begin(),nums.end());
       int sum0=nums.size();
       int len=nums.size();
       for (int i=0;i<nums.size()/2+1;i++){
           vector<int>::iterator st;
           st=nums.begin()+2*i;
           int end=nums.at(nums.size()-1);
    
           nums.insert(st,end);
           nums.resize(len);
           sum0++;
       }
       return;
    }
    

    };


  • 0
    A

    What is the function of sum0?


Log in to reply
 

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