You are given an integer array nums. Implement shiftZerosToEnd(nums). Return an array holding exactly the same values, rearranged so that every 0 comes after every non-zero value.
The non-zero values must keep their relative order: if one non-zero value appears before another non-zero value in nums, it still appears before it in the returned array. The zeros fill the remaining positions at the end, so the returned array has the same length as nums and only one arrangement is correct.
Examples
Example 1
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Explanation: The non-zero values are 1, 3 and 12, in that order; the two zeros follow them.
Example 2
Input: nums = [0]
Output: [0]
Explanation: There are no non-zero values, so the single zero is already at the end.
Constraints
1≤nums.length≤104
−231≤nums[i]≤231−1
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up