classSolution: defwiggleSort(self, nums: List[int]) -> None: n = len(nums) arr = sorted(nums) x = (n + 1) // 2 j, k = x - 1, n - 1 for i inrange(0, n, 2): nums[i] = arr[j] if i + 1 < n: nums[i + 1] = arr[k] j -= 1 k -= 1
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { publicvoidwiggleSort(int[] nums) { int[] arr = nums.clone(); Arrays.sort(arr); intn= nums.length; intx= (n + 1) / 2; for (inti=0, j = x - 1, k = n - 1; i < n; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < n) { nums[i + 1] = arr[k]; } } } }
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: voidwiggleSort(vector<int>& nums){ int n = nums.size(); vector<int> arr = nums; sort(arr.begin(), arr.end()); int x = (n + 1) / 2; for (int i = 0, j = x - 1, k = n - 1; i < n; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < n) { nums[i + 1] = arr[k]; } } } };
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicvoidWiggleSort(int[] nums) { int n = nums.Length; int[] arr = newint[n]; Array.Copy(nums, arr, n); Array.Sort(arr); int x = (n + 1) / 2; for (int i = 0, j = x - 1, k = n - 1; i < n; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < n) { nums[i + 1] = arr[k]; } } } }
voidwiggleSort(int* nums, int numsSize) { int * arr = (int *)malloc(sizeof(int) * numsSize); memcpy(arr, nums, sizeof(int) * numsSize); qsort(arr, numsSize, sizeof(int), cmp); int x = (numsSize + 1) / 2; for (int i = 0, j = x - 1, k = numsSize - 1; i < numsSize; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < numsSize) { nums[i + 1] = arr[k]; } } free(arr); }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var wiggleSort = function(nums) { const arr = nums.slice(); arr.sort((a, b) => a - b); const n = nums.length; const x = Math.floor((n + 1) / 2); for (let i = 0, j = x - 1, k = n - 1; i < n; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < n) { nums[i + 1] = arr[k]; } } };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcwiggleSort(nums []int) { n := len(nums) arr := append([]int{}, nums...) sort.Ints(arr) x := (n + 1) / 2 for i, j, k := 0, x-1, n-1; i < n; i += 2 { nums[i] = arr[j] if i+1 < n { nums[i+1] = arr[k] } j-- k-- } }
@staticmethod defpartition(nums: List, l: int, r: int) -> int: pivot = nums[r] i = l - 1 for j inrange(l, r): if nums[j] < pivot: i += 1 nums[i], nums[j] = nums[j], nums[i] nums[i + 1], nums[r] = nums[r], nums[i + 1] return i + 1
classSolution: defwiggleSort(self, nums: List[int]) -> None: n = len(nums) x = (n + 1) // 2 seed(datetime.datetime.now()) target = Helper.quickSelect(nums, 0, n - 1, x - 1) k, i, j = 0, 0, n - 1 while k <= j: if nums[k] > target: while j > k and nums[j] > target: j -= 1 nums[k], nums[j] = nums[j], nums[k] j -= 1 if nums[k] < target: nums[k], nums[i] = nums[i], nums[k] i += 1 k += 1 arr = nums.copy() j, k = x - 1, n - 1 for i inrange(0, n, 2): nums[i] = arr[j] if i + 1 < n: nums[i + 1] = arr[k] j -= 1 k -= 1
publicintquickSelect(int[] a, int l, int r, int index) { intq= randomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index); } }
publicintrandomPartition(int[] a, int l, int r) { inti= random.nextInt(r - l + 1) + l; swap(a, i, r); return partition(a, l, r); }
publicintpartition(int[] a, int l, int r) { intx= a[r], i = l - 1; for (intj= l; j < r; ++j) { if (a[j] <= x) { swap(a, ++i, j); } } swap(a, i + 1, r); return i + 1; }
publicvoidswap(int[] a, int i, int j) { inttemp= a[i]; a[i] = a[j]; a[j] = temp; } }
publicclassSolution { Random random = new Random();
publicvoidWiggleSort(int[] nums) { int n = nums.Length; int x = (n + 1) / 2; int mid = x - 1; int target = FindKthLargest(nums, n - mid); for (int k = 0, i = 0, j = n - 1; k <= j; k++) { if (nums[k] > target) { while (j > k && nums[j] > target) { j--; } Swap(nums, k, j--); } if (nums[k] < target) { Swap(nums, k, i++); } } int[] arr = newint[n]; Array.Copy(nums, arr, n); for (int i = 0, j = x - 1, k = n - 1; i < n; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < n) { nums[i + 1] = arr[k]; } } }
publicintQuickSelect(int[] a, int l, int r, int index) { int q = RandomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? QuickSelect(a, q + 1, r, index) : QuickSelect(a, l, q - 1, index); } }
publicintRandomPartition(int[] a, int l, int r) { int i = random.Next(r - l + 1) + l; Swap(a, i, r); return Partition(a, l, r); }
publicintPartition(int[] a, int l, int r) { int x = a[r], i = l - 1; for (int j = l; j < r; ++j) { if (a[j] <= x) { Swap(a, ++i, j); } } Swap(a, i + 1, r); return i + 1; }
publicvoidSwap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } }
staticinlinevoidswap(int* a, int* b) { int t = *a; *a = *b; *b = t; }
staticinlineintpartition(int* a, int l, int r) { int x = a[r], i = l - 1; for (int j = l; j < r; ++j) { if (a[j] <= x) { swap(&a[++i], &a[j]); } } swap(&a[i + 1], &a[r]); return i + 1; }
staticinlineintrandomPartition(int* a, int l, int r) { int i = rand() % (r - l + 1) + l; swap(&a[i], &a[r]); return partition(a, l, r); }
staticintquickSelect(int* a, int l, int r, int index) { int q = randomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index); } }
staticintfindKthLargest(int* nums, int numsSize, int k) { srand(time(0)); return quickSelect(nums, 0, numsSize - 1, numsSize - k); }
voidwiggleSort(int* nums, int numsSize) { int x = (numsSize + 1) / 2; int mid = x - 1; int target = findKthLargest(nums, numsSize, numsSize - mid); for (int k = 0, i = 0, j = numsSize - 1; k <= j; k++) { if (nums[k] > target) { while (j > k && nums[j] > target) { j--; } swap(&nums[k], &nums[j--]); } if (nums[k] < target) { swap(&nums[k], &nums[i++]); } } int *arr = (int *)malloc(sizeof(int) * numsSize); memcpy(arr, nums, sizeof(int) * numsSize); for (int i = 0, j = x - 1, k = numsSize - 1; i < numsSize; i += 2, j--, k--) { nums[i] = arr[j]; if (i + 1 < numsSize) { nums[i + 1] = arr[k]; } } free(arr); }
publicintquickSelect(int[] a, int l, int r, int index) { intq= randomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index); } }
publicintrandomPartition(int[] a, int l, int r) { inti= random.nextInt(r - l + 1) + l; swap(a, i, r); return partition(a, l, r); }
publicintpartition(int[] a, int l, int r) { intx= a[r], i = l - 1; for (intj= l; j < r; ++j) { if (a[j] <= x) { swap(a, ++i, j); } } swap(a, i + 1, r); return i + 1; }
publicvoidswap(int[] a, int i, int j) { inttemp= a[i]; a[i] = a[j]; a[j] = temp; } }
publicclassSolution { Random random = new Random();
publicvoidWiggleSort(int[] nums) { int n = nums.Length; int x = (n + 1) / 2; int mid = x - 1; int target = FindKthLargest(nums, n - mid); for (int k = 0, i = 0, j = n - 1; k <= j; k++) { if (nums[TransAddress(k, n)] > target) { while (j > k && nums[TransAddress(j, n)] > target) { j--; } Swap(nums, TransAddress(k, n), TransAddress(j--, n)); } if (nums[TransAddress(k, n)] < target) { Swap(nums, TransAddress(k, n), TransAddress(i++, n)); } } }
publicintTransAddress(int i, int n) { return (2 * n - 2 * i - 1) % (n | 1); }
publicintQuickSelect(int[] a, int l, int r, int index) { int q = randomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? QuickSelect(a, q + 1, r, index) : QuickSelect(a, l, q - 1, index); } }
publicintrandomPartition(int[] a, int l, int r) { int i = random.Next(r - l + 1) + l; Swap(a, i, r); return Partition(a, l, r); }
publicintPartition(int[] a, int l, int r) { int x = a[r], i = l - 1; for (int j = l; j < r; ++j) { if (a[j] <= x) { Swap(a, ++i, j); } } Swap(a, i + 1, r); return i + 1; }
publicvoidSwap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } }
staticinlinevoidswap(int* a, int* b) { int t = *a; *a = *b; *b = t; }
staticinlineintpartition(int* a, int l, int r) { int x = a[r], i = l - 1; for (int j = l; j < r; ++j) { if (a[j] <= x) { swap(&a[++i], &a[j]); } } swap(&a[i + 1], &a[r]); return i + 1; }
staticinlineintrandomPartition(int* a, int l, int r) { int i = rand() % (r - l + 1) + l; swap(&a[i], &a[r]); return partition(a, l, r); }
staticintquickSelect(int* a, int l, int r, int index) { int q = randomPartition(a, l, r); if (q == index) { return a[q]; } else { return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index); } }
staticintfindKthLargest(int* nums, int numsSize, int k) { srand(time(0)); return quickSelect(nums, 0, numsSize - 1, numsSize - k); }
staticinlineinttransAddress(int i, int n) { return (2 * n - 2 * i - 1) % (n | 1); }
voidwiggleSort(int* nums, int numsSize) { int x = (numsSize + 1) / 2; int mid = x - 1; int target = findKthLargest(nums, numsSize, numsSize - mid); for (int k = 0, i = 0, j = numsSize - 1; k <= j; k++) { if (nums[transAddress(k, numsSize)] > target) { while (j > k && nums[transAddress(j, numsSize)] > target) { j--; } swap(&nums[transAddress(k, numsSize)], &nums[transAddress(j--, numsSize)]); } if (nums[transAddress(k, numsSize)] < target) { swap(&nums[transAddress(k, numsSize)], &nums[transAddress(i++, numsSize)]); } } }
defquickSelect(a: List[int], k: int) -> int: seed(datetime.datetime.now()) shuffle(a) l, r = 0, len(a) - 1 while l < r: pivot = a[l] i, j = l, r + 1 whileTrue: i += 1 while i < r and a[i] < pivot: i += 1 j -= 1 while j > l and a[j] > pivot: j -= 1 if i >= j: break a[i], a[j] = a[j], a[i] a[l], a[j] = a[j], pivot if j == k: break if j < k: l = j + 1 else: r = j - 1 return a[k]
classSolution: defwiggleSort(self, nums: List[int]) -> None: n = len(nums) x = (n + 1) // 2 target = quickSelect(nums, x - 1)
transAddress = lambda i: (2 * n - 2 * i - 1) % (n | 1) k, i, j = 0, 0, n - 1 while k <= j: tk = transAddress(k) if nums[tk] > target: while j > k and nums[transAddress(j)] > target: j -= 1 tj = transAddress(j) nums[tk], nums[tj] = nums[tj], nums[tk] j -= 1 if nums[tk] < target: ti = transAddress(i) nums[tk], nums[ti] = nums[ti], nums[tk] i += 1 k += 1
classSolution { public: intpartitionAroundPivot(int left, int right, int pivot, vector<int> &nums){ int pivotValue = nums[pivot]; int newPivot = left; swap(nums[pivot], nums[right]); for (int i = left; i < right; ++i) { if (nums[i] > pivotValue) { swap(nums[i], nums[newPivot++]); } } swap(nums[right], nums[newPivot]); return newPivot; }
intfindKthLargest(vector<int> &nums, int k){ int left = 0, right = nums.size() - 1; default_random_engine gen((random_device())()); while (left <= right) { uniform_int_distribution<int> dis(left, right); int pivot = dis(gen); int newPivot = partitionAroundPivot(left, right, pivot, nums); if (newPivot == k - 1) { return nums[newPivot]; } elseif (newPivot > k - 1) { right = newPivot - 1; } else { left = newPivot + 1; } } return nums[k - 1]; }
inlineinttransAddress(int i, int n){ return (2 * n - 2 * i - 1) % (n | 1); }
voidwiggleSort(vector<int>& nums){ int n = nums.size(); int x = (n + 1) / 2; int mid = x - 1; int target = findKthLargest(nums, n - mid); for (int k = 0, i = 0, j = n - 1; k <= j; k++) { if (nums[transAddress(k, n)] > target) { while (j > k && nums[transAddress(j, n)] > target) { j--; } swap(nums[transAddress(k, n)], nums[transAddress(j--, n)]); } if (nums[transAddress(k, n)] < target) { swap(nums[transAddress(k, n)], nums[transAddress(i++, n)]); } } } };
publicclassSolution { Random random = new Random();
publicvoidWiggleSort(int[] nums) { int n = nums.Length; int x = (n + 1) / 2; int mid = x - 1; int target = FindKthLargest(nums, n - mid); for (int k = 0, i = 0, j = n - 1; k <= j; k++) { if (nums[TransAddress(k, n)] > target) { while (j > k && nums[TransAddress(j, n)] > target) { j--; } Swap(nums, TransAddress(k, n), TransAddress(j--, n)); } if (nums[TransAddress(k, n)] < target) { Swap(nums, TransAddress(k, n), TransAddress(i++, n)); } } }
publicintFindKthLargest(int[] nums, int k) { int left = 0, right = nums.Length - 1; while (left <= right) { int pivot = random.Next(right - left + 1) + left; int newPivot = PartitionAroundPivot(left, right, pivot, nums); if (newPivot == k - 1) { return nums[newPivot]; } elseif (newPivot > k - 1) { right = newPivot - 1; } else { left = newPivot + 1; } } return nums[k - 1]; }
publicintTransAddress(int i, int n) { return (2 * n - 2 * i - 1) % (n | 1); }
publicintPartitionAroundPivot(int left, int right, int pivot, int[] nums) { int pivotValue = nums[pivot]; int newPivot = left; Swap(nums, pivot, right); for (int i = left; i < right; ++i) { if (nums[i] > pivotValue) { Swap(nums, i, newPivot++); } } Swap(nums, right, newPivot); return newPivot; }
publicvoidSwap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } }
staticinlinevoidswap(int *a, int *b) { int c = *a; *a = *b; *b = c; }
staticinlineintpartitionAroundPivot(int left, int right, int pivot, int *nums) { int pivotValue = nums[pivot]; int newPivot = left; swap(&nums[pivot], &nums[right]); for (int i = left; i < right; ++i) { if (nums[i] > pivotValue) { swap(&nums[i], &nums[newPivot++]); } } swap(&nums[right], &nums[newPivot]); return newPivot; }
staticintfindKthLargest(int* nums, int numsSize, int k) { int left = 0, right = numsSize - 1; srand(time(0)); while (left <= right) { int pivot = rand() % (right - left + 1) + left; int newPivot = partitionAroundPivot(left, right, pivot, nums); if (newPivot == k - 1) { return nums[newPivot]; } elseif (newPivot > k - 1) { right = newPivot - 1; } else { left = newPivot + 1; } } return nums[k - 1]; }
staticinlineinttransAddress(int i, int n) { return (2 * n - 2 * i - 1) % (n | 1); }
voidwiggleSort(int* nums, int numsSize) { int x = (numsSize + 1) / 2; int mid = x - 1; int target = findKthLargest(nums, numsSize, numsSize - mid); for (int k = 0, i = 0, j = numsSize - 1; k <= j; k++) { if (nums[transAddress(k, numsSize)] > target) { while (j > k && nums[transAddress(j, numsSize)] > target) { j--; } swap(&nums[transAddress(k, numsSize)], &nums[transAddress(j--, numsSize)]); } if (nums[transAddress(k, numsSize)] < target) { swap(&nums[transAddress(k, numsSize)], &nums[transAddress(i++, numsSize)]); } } }