|
| 1 | +--- |
| 2 | +id: move-zeroes |
| 3 | +title: Move Zeroes(LeetCode) |
| 4 | +sidebar_label: 0283-Move Zeroes |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | +description: Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Statement |
| 12 | + |
| 13 | +Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements. |
| 14 | + |
| 15 | +Note that you must do this in-place without making a copy of the array. |
| 16 | + |
| 17 | +### Examples |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +```plaintext |
| 22 | +Input: nums = [0,1,0,3,12] |
| 23 | +Output: [1,3,12,0,0] |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +```plaintext |
| 29 | +Input: nums = [0] |
| 30 | +Output: [0] |
| 31 | +``` |
| 32 | + |
| 33 | +### Constraints |
| 34 | + |
| 35 | +- `1 <= nums.length <= 104` |
| 36 | +- `231 <= nums[i] <= 231 - 1` |
| 37 | + |
| 38 | +## Solution |
| 39 | + |
| 40 | +### Explanation |
| 41 | +The algorithm employs a two-pointer approach with a `slow` pointer and a `fast` pointer to achieve this task efficiently. |
| 42 | + |
| 43 | +1. Initialization: |
| 44 | +* Initialize the `slow` pointer to 0. The `slow` pointer will track the position to place the next non-zero element. |
| 45 | +* The `fast` pointer will traverse the entire array. |
| 46 | +2. Traversal: |
| 47 | +* Iterate through the array using the `fast` pointer. |
| 48 | +* For each element: |
| 49 | + * If `nums[fast]` is not zero and `nums[slow]` is zero, swap the elements at the `slow` and `fast` pointers. |
| 50 | + * Increment the `slow` pointer if it points to a non-zero element. |
| 51 | +3. Swapping: |
| 52 | +* This swapping ensures that all non-zero elements are moved to the front of the array, and the zeros are moved to the back. |
| 53 | + |
| 54 | +### Algorithm |
| 55 | + |
| 56 | +1. Initialize the `slow` pointer to 0. |
| 57 | +2. Iterate through the array with the `fast` pointer from index 0 to the end of the array: |
| 58 | +* If `nums[fast]` is not zero and `nums[slow]` is zero: |
| 59 | + * Swap `nums[slow]` and `nums[fast]`. |
| 60 | +* If `nums[slow]` is not zero, increment the `slow` pointer. |
| 61 | + |
| 62 | +### Implementation |
| 63 | + |
| 64 | +```Python |
| 65 | +class Solution: |
| 66 | + def moveZeroes(self, nums: list) -> None: |
| 67 | + slow = 0 |
| 68 | + for fast in range(len(nums)): |
| 69 | + if nums[fast] != 0 and nums[slow] == 0: |
| 70 | + nums[slow], nums[fast] = nums[fast], nums[slow] |
| 71 | + |
| 72 | + # wait while we find a non-zero element to |
| 73 | + # swap with you |
| 74 | + if nums[slow] != 0: |
| 75 | + slow += 1 |
| 76 | +``` |
| 77 | + |
| 78 | +### Complexity Analysis |
| 79 | + |
| 80 | +- **Time complexity**: O(n), where n is the number of elements in the array. Each element is processed once by the `fast` pointer. |
| 81 | +- **Space complexity**: O(1), as the operations are performed in-place without using extra space. |
| 82 | + |
| 83 | +### Conclusion |
| 84 | + |
| 85 | +This approach ensures that all the zeros in the array are moved to the end while maintaining the order of the non-zero elements. The in-place operations guarantee an O(1) space complexity, and each element is processed only once, resulting in an O(n) time complexity. This solution is optimal and efficient for the given problem. |
0 commit comments