본문 바로가기

IT/코딩테스트

[Leetcode] 922. Sort Array By Parity II

728x90

https://leetcode.com/problems/sort-array-by-parity-ii/

class Solution:
    def sortArrayByParityII(self, nums: List[int]) -> List[int]:
        result = []
        even_index = 0
        odd_index = 1

        for i in nums:
            if i % 2 == 0:
                result.insert(even_index, i)
                even_index += 2
            else:
                result.insert(odd_index, i)
                odd_index += 2

        return result

 

728x90