파비의 매일매일 공부기록

2023.02.06 Today's Challenge 본문

Problem Solving/LeetCode

2023.02.06 Today's Challenge

fabichoi 2023. 2. 6. 23:45

https://leetcode.com/problems/shuffle-the-array/

 

Shuffle the Array - LeetCode

Shuffle the Array - 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

leetcode.com

오랫만에 간단히 풀 수 있는 문제.
배열을 두 부분으로 나눠서 원본에 저장하면 됨.
좀 더 간단하게 구현할 수 있는 방법이 있긴 할 듯.

class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:        
        n1, n2 = nums[:n], nums[n:]
        for i in range(n):
            nums[i*2] = n1[i]
            nums[i*2+1] = n2[i]
        return nums
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.02.08 Today's Challenge  (0) 2023.02.08
2023.02.07 Today's Challenge  (0) 2023.02.07
2023.02.05 Today's Challenge  (0) 2023.02.05
2023.02.04 Today's Challenge  (0) 2023.02.04
2023.02.03 Today's Challenge  (0) 2023.02.03
Comments