507.Wiggle Sort II

public class Solution {
/**
 * @param nums a list of integer
 * @return void
 */
public void wiggleSort(int[] nums) {
    // Write your code here
    Arrays.sort(nums);
    int[] copyArray = Arrays.copyOf(nums, nums.length);
    int indexA = nums.length - 1;
    int indexB = 0;
    for (int i = 1; i < nums.length; i++) {
        if (i % 2 == 1) {
            nums[i] = copyArray[indexA];
            indexA--;
        }
    }
    for (int i = nums.length - 1; i >= 0; i--) {
        if (i % 2 == 0) {
            nums[i] = copyArray[indexB];
            indexB++;
        }
    }
}
}

你可能感兴趣的:(507.Wiggle Sort II)