5 lines c++ solution, easy understanding


  • 1
    L
     void wiggleSort(vector<int>& nums) {
        if (nums.size() <= 1) return;
        bool inc = true;
        for (int i = 1; i < nums.size(); i++) {
           if ((inc && nums[i-1] > nums[i]) || (!inc && nums[i-1] < nums[i])) swap(nums[i], nums[i-1]);
          inc = !inc;
      }
    }

Log in to reply
 

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