Advertisement
kosievdmerwe

Untitled

Sep 28th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. class Solution:
  2.     def sortArrayByParityII(self, nums: List[int]) -> List[int]:
  3.         N = len(nums)
  4.         even_idx = 0
  5.         odd_idx = 1
  6.         while True:
  7.             while even_idx < N and nums[even_idx] % 2 == 0:
  8.                 even_idx += 2
  9.             while odd_idx < N and nums[odd_idx] % 2 == 1:
  10.                 odd_idx += 2
  11.             if even_idx >= N:
  12.                 break
  13.             nums[even_idx], nums[odd_idx] = nums[odd_idx], nums[even_idx]
  14.         return nums
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement