一、问题描述
情景,给定一个数组,从头开始遍历,第一位的元素输出,下一位移动到队尾,直到所有元素输出,
二、Code
1 public class QueueOperation { 2 /* 3 思路 4 1、换一种表达方式就是偶数位的元素输出,奇数位的元素移动到队尾。 5 2、思路,用head索引输出值,然后用tail索引放入存入值 6 3、插入次数 7 x + n = 2x 8 x = n 9 */ 10 public List<Integer> queueOperation(int[] inputs) { 11 int head = 0, n = inputs.length, tail = n-1; 12 int[] nums = new int[n + n]; 13 System.arraycopy(inputs, 0, nums, 0, n); 14 List<Integer> res = new ArrayList<>(); 15 16 while (head <= tail) { 17 res.add(nums[head++]); 18 nums[++tail] = nums[head++]; 19 } 20 return res; 21 } 22 }