class Solution {
public:
void wiggleSort(vector<int>& nums) {
if (nums.size() <= 1)
{
return;
}
sort(nums.begin(), nums.end());
int right = nums.size() - 1;
int start = right / 2;
int left = start - 1;
vector<int> ret;
ret.push_back(nums[start]);
while (ret.size() < nums.size())
{
if (ret.size() < nums.size())
ret.push_back(nums[right--]);
if (ret.size() < nums.size())
ret.push_back(nums[left--]);
}
nums = ret;
}
};
public:
void wiggleSort(vector<int>& nums) {
if (nums.size() <= 1)
{
return;
}
sort(nums.begin(), nums.end());
int right = nums.size() - 1;
int start = right / 2;
int left = start - 1;
vector<int> ret;
ret.push_back(nums[start]);
while (ret.size() < nums.size())
{
if (ret.size() < nums.size())
ret.push_back(nums[right--]);
if (ret.size() < nums.size())
ret.push_back(nums[left--]);
}
nums = ret;
}
};
评论
发表评论