My simple c++ solution


  • 1
    K
    class Solution {
    public:
    void wiggleSort(vector<int>& nums) {
        sort(nums.begin(), nums.end());//[3, 5, 2, 1, 6, 4]->1,2,3,5,4,6->
        for(int i=2;i< nums.size();i=i+2){
            int temp=nums[i];
            nums[i]=nums[i-1];
            nums[i-1]=temp;
        }
    }
    

    };


  • 0
    H

    The solution using sort is O(n log n). The best solution should be O(n).


  • 0
    V

    so ? in my opinion this kind of algorithm is much more programming oriented than the ones using math tricks


Log in to reply
 

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