solution.cpp 420 字节
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    void wiggleSort(vector<int> &nums)
    {
        int n = nums.size();
        vector<int> tmp(nums);
        sort(tmp.begin(), tmp.end());
        int mid = n / 2, end = n - 1;
        if (n % 2 == 0)
            mid--;
        for (int i = 0; i < n; i++)
        {
            nums[i] = i % 2 == 0 ? tmp[mid--] : tmp[end--];
        }
    }
};