1 public class Solution { 2 public void wiggleSort(int[] nums) { 3 Arrays.sort(nums); 4 5 int[] result = new int[nums.length]; 6 int bound = nums.length % 2 == 1 ? nums.length / 2 : nums.length / 2 - 1; 7 int i = bound; 8 int j = nums.length - 1; 9 int index = 0; 10 11 while (i <= bound) { 12 result[index++] = nums[i--]; 13 if (j > bound) { 14 result[index++] = nums[j--]; 15 } 16 } 17 18 for (i = 0; i < nums.length; i++) { 19 nums[i] = result[i]; 20 } 21 } 22 }
1. Use backward arrangement in case of [4, 5, 5, 6].
时间: 2024-12-28 20:43:18