Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public void moveZeroes(int[] nums) {
- if (nums == null || nums.length == 0) {
- return;
- }
- // Insert non-zero values at the beginning
- // of the array, one-by-one.
- int insertPosition = 0;
- for (int num: nums) {
- if (num != 0) {
- nums[insertPosition++] = num;
- }
- }
- // Fill the rest of the positions with 0.
- while (insertPosition < nums.length) {
- nums[insertPosition++] = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement