작심삼일

[LeetCode] 905 | Sort Array By Parity | Python 본문

스터디/코테

[LeetCode] 905 | Sort Array By Parity | Python

yun_s 2022. 5. 2. 09:28
728x90
반응형

문제 링크: https://leetcode.com/problems/sort-array-by-parity/


문제

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.


조건

  • 1 <= nums.length <= 5000
  • 0 <= nums[i] <= 5000

내 풀이

Python에서 리스트를 key로 정리하는 방법을 사용한다.

key를 이용해 짝수인 경우 앞으로 배치되게 한다.


코드

class Solution:
    def sortArrayByParity(self, nums: List[int]) -> List[int]:
        return sorted(nums, key=lambda x: x%2)
728x90
반응형
Comments