给你一个数组 nums ,数组中有 2n 个元素,按 [x1,x2,...,xn,y1,y2,...,yn] 的格式排列。 请你将数组按 [x1,y1,x2,y2,...,xn,yn] 格式重新排列

执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户 内存消耗:38.7 MB, 在所有 Java 提交中击败了94.78% 的用户

代码

class Solution {
    public int[] shuffle(int[] nums, int n) {
        int s = n-1;
        int []res = new int[2*n];
        for(int i=0;i<2*n;i+=2){
            res[i] = nums[i/2];
            s++;
            res[i+1] = nums[s];
        }
         return res;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45942124/article/details/108875951