重新排序数组
package weekly_contest_192;
import java.util.Arrays;
public class Solution {
public int[] shuffle(int[] nums, int n) {
int[] ans = new int[nums.length];
int[] tempY = new int[n];
int[] tempX = new int[n];
for (int i = 0; i < n; i++) {
tempX[i] = nums[i];
tempY[i] = nums[i+n];
}
for (int i = 0,j=0; i < n; i++,j=j+2) {
ans[j] = tempX[i];
ans[j+1] = tempY[i];
}
return ans;
}
public static void main(String[] args) {
int[] nums = {1,2,3,4,4,3,2,1};
int[] res = new Solution().shuffle(nums, 4);
System.out.println(Arrays.toString(res));
}
}