TheAlgorithms-C/leetcode/src/1089.c

23 lines
460 B
C
Raw Normal View History

void duplicateZeros(int *arr, int arrSize)
{
2019-10-11 02:34:58 +08:00
int i, start = 0;
int *tmp = malloc(arrSize * sizeof(int));
/* Copy arr into tmp arr */
for (i = 0; i < arrSize; i++)
{
2019-10-11 02:34:58 +08:00
tmp[i] = arr[i];
}
i = 0;
for (start = 0; start < arrSize; start++)
{
2019-10-11 02:34:58 +08:00
arr[start] = tmp[i];
if (tmp[i] == 0)
{
2019-10-11 02:34:58 +08:00
start++;
if (start < arrSize)
arr[start] = 0;
}
i++;
}
}