LeetCode—905. Sort Array By Parity


//url:https://leetcode.com/problems/sort-array-by-parity/

func sortArrayByParity(A []int) []int {
	startIndex := 0
	endIndex := len(A) - 1
	for startIndex < endIndex {
		if 0 != A[startIndex]%2 {
			for startIndex < endIndex {
				if 0 == A[endIndex]%2 {
					fmt.Println("startIndex=", startIndex, ",endIndex=", endIndex, "swap:", A[startIndex], ",", A[endIndex])
					tmp := A[startIndex]
					A[startIndex] = A[endIndex]
					A[endIndex] = tmp
					endIndex--
					break
				}
				endIndex--
			}
		}
		startIndex++
	}
	return A
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注