-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path283_Move_Zeroes.py
More file actions
46 lines (28 loc) · 1.13 KB
/
283_Move_Zeroes.py
File metadata and controls
46 lines (28 loc) · 1.13 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
39
40
41
42
43
44
'''
Problem Statement:
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
1: You must do this in-place without making a copy of the array.
2: Minimize the total number of operations.
Approach:
* We should have count of number of zeros in the provided list
* We will keep a counter of index that is zero
* We will iterate through the list and once the value is non-zero, then we will be swap the non-zero value with zero value and increment the counter next zero.
* Finally we will add zero's at the end of the list based on number of zero's we have counted in the first step
'''
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
count_zero = nums.count(0)
next_zero_index = 0
for n in nums:
if n!=0:
nums[next_zero_index] = n
next_zero_index += 1
for i in range(1, count_zero +1):
nums[-i] = 0