Skip to content

Commit f8ff7fa

Browse files
committed
improved move_zeroes
1 parent 24d5f7d commit f8ff7fa

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

2024/move_zeroes.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class Solution(object):
22
def moveZeroes(self, nums):
3-
x = 0
4-
y = len(nums)
5-
while x < y:
6-
if nums[x] == 0:
7-
nums.pop(x)
8-
nums.append(0)
9-
y = y - 1
10-
else:
11-
x = x + 1
3+
nums_length = len(nums)
4+
if nums_length <= 1:
5+
return
6+
7+
bound = 0
8+
for i in range(1, nums_length):
9+
if nums[bound] != 0:
10+
bound += 1
11+
if nums[i] != 0:
12+
nums[bound], nums[i] = nums[i], nums[bound]

0 commit comments

Comments
 (0)