重新排序數組

重新排序數組

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 = {2,5,1,3,4,7};//-------------------->2,3,5,4,1,7
//    	int[] res = new Solution().shuffle(nums, 3);
		int[] nums = {1,2,3,4,4,3,2,1};//---------------->1,4,2,3,3,2,4,1
		int[] res = new Solution().shuffle(nums, 4);
		System.out.println(Arrays.toString(res));
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章