Practice/Meta/Leetcode 283. Move Zeroes
CodingMust
Given an array of integers nums and a target value target, modify the array in-place so that all occurrences of the target value are moved to the end of the array. The relative order of all other elements must be preserved.
You must solve this without creating a copy of the array. The function should modify the input array directly and return it.
nums.length ≤ 10⁴nums[i] ≤ 10⁹target ≤ 10⁹Example 1:
Input: nums = [4, 2, 4, 0, 0, 3, 0, 5, 1, 0], target = 0 Output: [4, 2, 4, 3, 5, 1, 0, 0, 0, 0] Explanation: All zeros are pushed to the end, while other elements maintain their original order (4, 2, 4, 3, 5, 1).
Example 2:
Input: nums = [1, 2, 3, 4, 5], target = 0 Output: [1, 2, 3, 4, 5] Explanation: No target elements exist, so the array remains unchanged.
Example 3:
Input: nums = [5, 5, 5], target = 5 Output: [5, 5, 5] Explanation: All elements are the target, so the array remains unchanged.