반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 905 | Sort Array By Parity | Python 본문
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
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1679 | Max Number of K-Sum Pairs | Python (0) | 2022.05.04 |
---|---|
[LeetCode] 581 | Shortest Unsorted Continuous Subarray | Python (0) | 2022.05.03 |
[LeetCode] 1631 | Path With Minimum Effort | Python (0) | 2022.04.28 |
[LeetCode] 1202 | Smallest String With Swaps | Python (0) | 2022.04.27 |
[LeetCode] 1584 | Min Cost to Connect All Points | Python (0) | 2022.04.26 |
Comments