class Solution {
public void wiggleSort(int[] nums) {
Arrays.sort(nums);
for(int i = 0;i < nums.length - 1;i+=2){
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
}
int compare(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
void wiggleSort(int* nums, int numsSize)
{
qsort(nums, numsSize, sizeof(int), compare);
for(int i = 0;i < numsSize - 1;i += 2)
{
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
class Solution:
def wiggleSort(self, nums: List[int]) -> None:
nums.sort()
for i in range(0, len(nums) - 1, 2):
temp = nums[i]
nums[i] = nums[i + 1]
nums[i + 1] = temp
class Solution {
public:
void wiggleSort(vector<int>& nums) {
if(nums.size() == 0){
return;
}
sort(nums.begin(), nums.end());
for(int i = 0;i < nums.size() - 1;i += 2){
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
}
}
};
Java语言版
C语言版
C++语言版