重排

题目

代码

public class Solution {
    /**
     * @param nums: the num arrays 
     * @return: the num arrays after rearranging
     */
    public int[] rearrange(int[] nums) {
        // Write your code here
        if(nums==null||nums.length<=0){
            return null;
        }
        Arrays.sort(nums);
        int[] array = new int[nums.length];
        int index1=0;
        int index2=nums.length/2;
        for(int i=0;index1<nums.length/2;i+=2){
            array[i]=nums[index1++];
            array[i+1]=nums[index2++];
        }
        array[nums.length-1]=nums[nums.length-1];
        return array;
    }
}
posted @ 2018-04-07 15:26  baixiaoshuai  阅读(152)  评论(0编辑  收藏  举报