[leetcode] 2
LeetCode 1470. Shuffle the Array
Problem
Given the array nums consisting of 2n elements in the form [x1,x2,…,xn,y1,y2,…,yn].
Return the array in the form [x1,y1,x2,y2,…,xn,yn].
Example
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
나의 해석
2n의 길이를 갖는 Array nums가 새로운 결과값으로 result[]= nums[0], nums[n], nums[1], nums[n+1]… nums[n], nums[2n]의 형태를 원하는 문제이다.
class Solution {
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]; 0,1 / 2,3 / 4,5 로 배열의 값으 넣어줘야 하기때문에
result[2*i+1]=nums[n+i]; result배열의 index를 2*i로 하였다.
}
return result;
}
}