leetcode-922 odd and even sorted array II

leetcode-922 odd and even sorted array II

Subject description:

Given a non-negative integer array A, the A half of the integer is an odd number, an even number of half integer. Sort the array, such that when A [i] is an odd number, I is an odd number; when A [i] is an even number, I is an even number.

Solution one:

class Solution:
    def sortArrayByParityII(self, A: List[int]) -> List[int]:
        i,j = 0,0
        n = len(A)
        indx = 0
        while i < n and j<n:
            while j<n and A[j]%2 == 1:
                j += 1
            while i < n and A[i]%2 == 0:
                i += 1
            if indx % 2 == 0:
                A[indx],A[j] = A[j], A[indx]
                indx += 1
                j += 1
            else:
                A[indx],A[i] = A[i], A[indx]
                indx += 1
                i += 1
        return A

Guess you like

Origin www.cnblogs.com/curtisxiao/p/11241766.html