Leetcode Go Sort Array by Party II

Sort Array By Parity II - LeetCode
https://leetcode.com/problems/sort-array-by-parity-ii/

  • Go 言語で取り組んだメモ

回答

Go one pass in-place superman solution - LeetCode Discuss
https://leetcode.com/problems/sort-array-by-parity-ii/discuss/715613/Go-one-pass-in-place-superman-solution

func sortArrayByParityII(A []int) []int {
    even, odd := 0, 1
    for even < len(A) && odd < len(A) {
        if A[even] % 2 != 0 {
            A[even], A[odd] = A[odd], A[even]
            odd += 2
        } else {
            even += 2
        }
    }
    return A
}