-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduplicateZeros.py
46 lines (43 loc) · 1.05 KB
/
duplicateZeros.py
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
39
40
41
42
43
44
45
46
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/duplicate-zeros/
# Author: Miao Zhang
# Date: 2021-04-07
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
i = 0
j = len(arr)
while i < j:
if arr[i] == 0:
arr.insert(i,0)
arr.pop()
i += 2
else:
i += 1
class Solution:
def duplicateZeros(self, arr: List[int]) -> None:
"""
Do not return anything, modify arr in-place instead.
"""
i = 0
j = 0
n = len(arr)
while i < n:
if arr[i] == 0:
j += 1
i += 1
j += 1
i -= 1
j -= 1
while i >= 0:
if j < n:
arr[j] = arr[i]
if arr[i] == 0:
j -= 1
if j < n:
arr[j] = 0
i -= 1
j -= 1