Notice
Recent Posts
Recent Comments
Link
오늘도 개발
10. Sort Array By Parity 본문
문제
https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3260/
nums 배열의 짝수 요소를 모두 앞으로 보내고, 그 다음 홀수 요소가 오도록 정렬하라.
Input: nums = [3,1,2,4]
Output: [2,4,3,1]
내가 해결한 방식
Move Zeros와 같은 방식으로 해결.
i는 짝수를 가리키는 포인터
odd_index는 홀수를 가리키는 포인터
def sortArrayByParity(self, nums: List[int]) -> List[int]:
odd_index = 0
for i in range(len(nums)):
# 짝수인 경우
if nums[i] % 2 == 0:
nums[odd_index], nums[i] = nums[i], nums[odd_index]
odd_index += 1
return nums'자료구조 & 알고리즘 > Leetcode' 카테고리의 다른 글
| 12. Height Checker (0) | 2022.06.03 |
|---|---|
| 11. Remove Element (0) | 2022.05.31 |
| 9. Move Zeros (0) | 2022.05.25 |
| 8. Replace Elements with Greatest Element on Right Side (0) | 2022.05.22 |
| 7. Valid Mountain Array (0) | 2022.05.20 |