forked from MukulCode/CodingClubIndia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDuplicate_Zeros.py
More file actions
38 lines (28 loc) · 1.02 KB
/
Duplicate_Zeros.py
File metadata and controls
38 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Given a fixed-length integer array arr, duplicate each occurrence of zero, shifting the remaining elements to the right.
# Note that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything.
# 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]
# Constraints:
# 1 <= arr.length <= 104
# 0 <= arr[i] <= 9
def duplicateZeros(arr):
"""
Do not return anything, modify arr in-place instead.
"""
i = 0
while i<len(arr)-1:
if arr[i] == 0:
arr.insert(i+1, 0)
del arr[len(arr)-1]
i += 2
else:
i+= 1
return arr
arr = [1,0,2,3,0,4,5,0]
print(duplicateZeros(arr))