Loading...
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.
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.
Input: nums = [0]
Output: [0]
Explanation: There are no non-zero values, so the single zero is already at the end.
nums.length ≤104nums[i] ≤231−1Click "Run" to test with sample cases or "Submit" to run all tests.