#1089

Duplicate Zeros

pupil · 450 · lc easy +26 · verified · 53.4% accepted · 2,832 likes · top 44%

Description

Given a fixed-length integer array arr, for each zero in the array insert a duplicate zero immediately after it, shifting all subsequent elements one position to the right. Elements that would be pushed beyond the array length are discarded.

Modify arr in place and return nothing.

Example 1:

Input: arr = [1,0,2,3,0,4,5,0]
Output: [1,0,0,2,3,0,0,4]
Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]

Example 2:

Input: arr = [1,2,3]
Output: [1,2,3]
Explanation: After calling your function, the input array is modified to: [1,2,3]

Code

1
2
3
4
5
6