luoheng
10/12/2019 - 6:20 AM

duplicateZeros

func duplicateZeros(arr []int)  {
    c, s, e := 0, 0, len(arr)-1
    for i, a := range arr {
        c++
        if a == 0 {
            c++
        }
        if c >= len(arr) {
            s = i
            if c > len(arr) {
                arr[e] = arr[s]
                s--
                e--
            }
            break
        }
    }
    for i, j := s, e; i >= 0 && i < j; i, j = i - 1, j - 1 {
        arr[j] = arr[i]
        if arr[i] == 0 {
            j--
            arr[j] = arr[i]
        }
    }
}