Open Tcdian opened 4 years ago
func duplicateZeros(arr []int) {
length := len(arr);
count := 0;
for i := 0; i < length; i++ {
if arr[i] == 0 {
count++;
}
}
copy := func(i int, j int) {
if j < length {
arr[j] = arr[i];
}
}
i, j := length - 1, length + count - 1;
for i < j {
copy(i, j);
if arr[i] == 0 {
j--;
copy(i, j);
}
i--;
j--;
}
}
1089. Duplicate Zeros
给你一个长度固定的整数数组 arr,请你将该数组中出现的每个零都复写一遍,并将其余的元素向右平移。
注意:请不要在超过该数组长度的位置写入元素。
要求:请对输入的数组 就地 进行上述修改,不要从函数返回任何东西。
Example 1
Example 2
Constraints
1 <= arr.length <= 10000
0 <= arr[i] <= 9