We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 764ea53 + 36eb0e3 commit d8a42efCopy full SHA for d8a42ef
1 file changed
movezero.py
@@ -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