Leetcode 1470. Shuffle the Array with Python

Alpha, Orderly·2023년 2월 6일
0

leetcode

목록 보기
43/90
post-thumbnail

문제

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].

2n의 길이를 가지고 형태가 [x1,x2,...,xn,y1,y2,...,yn] 와 같은 배열이 있다.

이 배열을 섞어 [x1,y1,x2,y2,...,xn,yn]과 같이 만든 후 리턴하시오.

예시

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7] 
Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]

제한

  • 1 <= n <= 500
  • nums.length == 2n
  • 1 <= nums[i] <= 10^3

풀이법

0 ~ n-1 Index의 값들은 기존 index i에 대해 i * 2 위치에 넣고

n ~ 2 n-1 Index의 값들은 기존 index i에 대해 (n-i) 2 + 1 위치에 넣어 리턴하면 된다

profile
만능 컴덕후 겸 번지 팬

0개의 댓글