LeetCode刷题:1470. 重新排列数组
给定数组,nums由2n形式为的元素组成。[x1,x2,…,xn,y1,y2,…,yn]
以形式返回数组 。[x1,y1,x2,y2,…,xn,yn]
范例1:
输入: nums = [2,5,1,3,4,7],n = 3
输出: [2,3,5,4,1,7]
说明:由于x 1 = 2,x 2 = 5,x 3 = 1,y 1 = 3,y 2 = 4,y 3 = 7,则答案为[2,3,5,4,1,7]。
范例2:
输入: nums = [1,2,3,4,4,3,2,1],n = 4
输出: [1,4,2,3,3,2,4,1]
范例3:
输入: nums = [1,1,2,2],n = 2
输出: [1,2,1,2]
限制条件:
1 <= n <= 500
nums.length == 2n
1 <= nums[i] <= 10^3
Java解题:
class Solution {
public int[] shuffle(int[] nums, int n) {
int [] a = new int [n * 2];
int j=0;
for(int i=0;i<n;i++){
a[j++]=nums[i];
a[j++]=nums[i+n];
}
return a;
}
}