Algorithm/LeetCode
[LeetCode][Easy][Java] 1470. Shuffle the Array
youn12
2021. 1. 11. 17:34
✏️ Algorithm.
- 주어진 하나의 배열을 x,y 두개의 배열로 나누어 각각 x,y 번갈아가며 값을 집어넣는다
- 주어진 규칙은 x1, y1, x2, y2 ... 형식이다.
📋 Solved.
1. 0~n 까지 반복(주어진 배열의 절반)
2. xi, yi 번갈아가며 값 집어넣기
✔️ Code.
public int[] shuffle(int[] nums, int n) {
int[] result = new int[nums.length];
for (int i = 0; i < n; i++) {
result[2*i] = nums[i]; //xi
result[2*i+1] = nums[i+n]; //yi
}
return result;
}