Advertisement
exmkg

3-6

Jan 14th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.55 KB | None | 0 0
  1. class Solution {
  2.     public void moveZeroes(int[] nums) {
  3.         if (nums == null || nums.length == 0) {
  4.             return;
  5.         }
  6.  
  7.         // Insert non-zero values at the beginning
  8.         // of the array, one-by-one.
  9.         int insertPosition = 0;
  10.         for (int num: nums) {
  11.             if (num != 0) {
  12.                 nums[insertPosition++] = num;
  13.             }
  14.         }        
  15.  
  16.         // Fill the rest of the positions with 0.
  17.         while (insertPosition < nums.length) {
  18.             nums[insertPosition++] = 0;
  19.         }
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement