move_zeroes

 1# @leet start
 2class Solution:
 3    def moveZeroes(self, nums: list[int]) -> None:
 4        """
 5        This question asks us to move the zeroes to the end of the array while
 6        keeping the relative order of the items in an array while using no
 7        extra space, and doing the mutation in place.
 8
 9        To do this, we can iterate through the array once, while keeping an index
10        to the last non-zero element.
11
12        If we find a non-zero element, we swap the current number with the number
13        at the index with zeroes, and advance both pointers.
14
15        If we find a zero, we just advance the current pointer.
16
17        Take an example of [0, 1, 0, 3, 12].
18        We initialize a slow pointer to 0, which holds the index of the last non-zero
19        number, and iterate through numbers
20
21        [0, 1, 0, 3, 12]
22         ^ we start off here, and find a 0, so we continue on.
23        slow = 0
24        [0, 1, 0, 3, 12]
25            ^ we find a 1, and so we swap it with our slow pointer and increment both.
26        slow = 0
27        [1, 0, 0, 3, 12]
28               ^ we find another 0, so we continue on.
29        slow = 1
30        [1, 0, 0, 3, 12]
31                  ^ we find a non-zero item, so we swap it with our slow pointer index.
32        slow = 1
33        [1, 3, 0, 0, 12]
34                      ^ we find a non-zero item, so we swap it with our slow pointer index.
35        slow = 2
36        [1, 3, 12, 0, 0]
37                      ^ we find a zero, so we do nothing.
38        slow = 3
39        """
40        pos = 0
41        for i, num in enumerate(nums):
42            if num != 0:
43                nums[pos], nums[i] = nums[i], nums[pos]
44                pos += 1
45
46
47# @leet end
48
49
50def test():
51    assert 2 + 2 == 4
class Solution:
 3class Solution:
 4    def moveZeroes(self, nums: list[int]) -> None:
 5        """
 6        This question asks us to move the zeroes to the end of the array while
 7        keeping the relative order of the items in an array while using no
 8        extra space, and doing the mutation in place.
 9
10        To do this, we can iterate through the array once, while keeping an index
11        to the last non-zero element.
12
13        If we find a non-zero element, we swap the current number with the number
14        at the index with zeroes, and advance both pointers.
15
16        If we find a zero, we just advance the current pointer.
17
18        Take an example of [0, 1, 0, 3, 12].
19        We initialize a slow pointer to 0, which holds the index of the last non-zero
20        number, and iterate through numbers
21
22        [0, 1, 0, 3, 12]
23         ^ we start off here, and find a 0, so we continue on.
24        slow = 0
25        [0, 1, 0, 3, 12]
26            ^ we find a 1, and so we swap it with our slow pointer and increment both.
27        slow = 0
28        [1, 0, 0, 3, 12]
29               ^ we find another 0, so we continue on.
30        slow = 1
31        [1, 0, 0, 3, 12]
32                  ^ we find a non-zero item, so we swap it with our slow pointer index.
33        slow = 1
34        [1, 3, 0, 0, 12]
35                      ^ we find a non-zero item, so we swap it with our slow pointer index.
36        slow = 2
37        [1, 3, 12, 0, 0]
38                      ^ we find a zero, so we do nothing.
39        slow = 3
40        """
41        pos = 0
42        for i, num in enumerate(nums):
43            if num != 0:
44                nums[pos], nums[i] = nums[i], nums[pos]
45                pos += 1
def moveZeroes(self, nums: list[int]) -> None:
 4    def moveZeroes(self, nums: list[int]) -> None:
 5        """
 6        This question asks us to move the zeroes to the end of the array while
 7        keeping the relative order of the items in an array while using no
 8        extra space, and doing the mutation in place.
 9
10        To do this, we can iterate through the array once, while keeping an index
11        to the last non-zero element.
12
13        If we find a non-zero element, we swap the current number with the number
14        at the index with zeroes, and advance both pointers.
15
16        If we find a zero, we just advance the current pointer.
17
18        Take an example of [0, 1, 0, 3, 12].
19        We initialize a slow pointer to 0, which holds the index of the last non-zero
20        number, and iterate through numbers
21
22        [0, 1, 0, 3, 12]
23         ^ we start off here, and find a 0, so we continue on.
24        slow = 0
25        [0, 1, 0, 3, 12]
26            ^ we find a 1, and so we swap it with our slow pointer and increment both.
27        slow = 0
28        [1, 0, 0, 3, 12]
29               ^ we find another 0, so we continue on.
30        slow = 1
31        [1, 0, 0, 3, 12]
32                  ^ we find a non-zero item, so we swap it with our slow pointer index.
33        slow = 1
34        [1, 3, 0, 0, 12]
35                      ^ we find a non-zero item, so we swap it with our slow pointer index.
36        slow = 2
37        [1, 3, 12, 0, 0]
38                      ^ we find a zero, so we do nothing.
39        slow = 3
40        """
41        pos = 0
42        for i, num in enumerate(nums):
43            if num != 0:
44                nums[pos], nums[i] = nums[i], nums[pos]
45                pos += 1

This question asks us to move the zeroes to the end of the array while keeping the relative order of the items in an array while using no extra space, and doing the mutation in place.

To do this, we can iterate through the array once, while keeping an index to the last non-zero element.

If we find a non-zero element, we swap the current number with the number at the index with zeroes, and advance both pointers.

If we find a zero, we just advance the current pointer.

Take an example of [0, 1, 0, 3, 12]. We initialize a slow pointer to 0, which holds the index of the last non-zero number, and iterate through numbers

[0, 1, 0, 3, 12] ^ we start off here, and find a 0, so we continue on. slow = 0 [0, 1, 0, 3, 12] ^ we find a 1, and so we swap it with our slow pointer and increment both. slow = 0 [1, 0, 0, 3, 12] ^ we find another 0, so we continue on. slow = 1 [1, 0, 0, 3, 12] ^ we find a non-zero item, so we swap it with our slow pointer index. slow = 1 [1, 3, 0, 0, 12] ^ we find a non-zero item, so we swap it with our slow pointer index. slow = 2 [1, 3, 12, 0, 0] ^ we find a zero, so we do nothing. slow = 3

def test():
51def test():
52    assert 2 + 2 == 4