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 1: 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]. Example 2: Input: nums = [1,2,3,4,4,3,2,1], n = 4 Output: [1,4,2,3,3,2,4,1] Example 3: Input: nums = [1,1,2,2], n = 2 Output: [1,2,1,2] |
Solution
class Solution: def shuffle(self, nums: List[int], n: int) -> List[int]: ans=[] for i in range(n): ans.append(nums[i]) ans.append(nums[i+n]) return ans |
네 번째 문제를 풀었다. 갈수록 힘든 문제다. 점점 빡친다. 그래도 해야지 ㅠㅠ
이번 문제는 n이다. n이 문제다. n은 무엇일까? 바로 순번이다.
힌트는 Explanation를 자세히 봐야한다. 1~3번 째 x 그리고 4~6번째는 y다. 이것을 섞어야한다.
처음엔 for문을 2번 써야하는 줄 알았다.
그러나 정답은 숫자를 순서대로 추가하는 방법이다.
ans의 빈 배열을 선언하고, n번째 기준으로 for문을 선언해준다.
ans.append(nums[i])
[2,5,1]이 출력된다.
ans.append(nums[i+n])
[3,4,7]이 출력된다.

이거 난이도가 쉬움이 맞나 싶다만... 뭐 쉬움이니까 받아들여야지
고로면 for문에서 순서대로 출력한 결과물은 [2,3,5,4,1,7]가 출력된다.
첫 번째 출력 [2,3]
두 번째 출력 [2,3,5,4]
세 번째 출력 [2,3,5,4,1,7]이 순서대로 출력된다.

그렇다면 오늘은 끝~
'초보코딩' 카테고리의 다른 글
LeetCode - 771. Jewels and Stones (0) | 2021.08.14 |
---|---|
LeetCode - 1431. Kids With the Greatest Number of Candies (0) | 2021.08.09 |
LeetCode - 1672. Richest Customer Wealth (0) | 2021.08.06 |
LeetCode - 1480. Running Sum of 1d Array (0) | 2021.08.05 |
LeetCode - 1920. Build Array from Permutation (0) | 2021.08.04 |