Wiggle Sort II

来源:互联网 发布:淘宝微任务 编辑:程序博客网 时间:2024/06/03 19:39

再接着想0(n)的解法。

public class Solution {    public void wiggleSort(int[] nums) {        if (nums == null || nums.length == 0) {            return;        }        Arrays.sort(nums);        int[] res = new int[nums.length];        for (int i = 0; i < nums.length; i++) {            res[i] = nums[i];        }        int mid = (nums.length - 1)/2, end = nums.length - 1;        for (int i = 0; i < nums.length;) {            if (mid >= 0) {                nums[i] = res[mid--];            }            i++;            if (end > (nums.length-1)/2) {                nums[i] = res[end--];            }            i++;        }    }}


0 0
原创粉丝点击