Skip to content

Commit d8a42ef

Browse files
authored
Merge pull request #1 from Photon079/Photon079-patch-1
Create movezero.py
2 parents 764ea53 + 36eb0e3 commit d8a42ef

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

movezero.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def move_zeros(nums: list[int]) -> list[int]:
2+
"""
3+
Moves all zeros in the list to the end while keeping other elements in order.
4+
"""
5+
last_non_zero = 0
6+
for i in range(len(nums)):
7+
if nums[i] != 0:
8+
nums[last_non_zero], nums[i] = nums[i], nums[last_non_zero]
9+
last_non_zero += 1
10+
return nums
11+
12+
if __name__ == "__main__":
13+
arr = [0, 1, 0, 3, 12]
14+
print("Before:", arr)
15+
print("After: ", move_zeros(arr))

0 commit comments

Comments
 (0)