code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

Movezerostoend

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

class MoveZerosToEnd {

  static int[] func(int[] nums){
    int count = 0;
    for(int i = 0; i < nums.length; i++){
      if(nums[i] != 0){
        int temp = nums[count];
        nums[count] = nums[i];
        nums[i] = temp;
        count += 1;
      }
    }
    return nums;
  }

  public static void main(String[] args){
    int[] nums = {10, 5, 0, 0, 8, 0, 9, 0};
    int[] result = func(nums);
    for(int i = 0; i < result.length; i++){
      System.out.println(result[i]);
    }
  }
}

Created 2021-10-27T02:53:48+01:00, updated 2021-10-27T13:06:06+01:00 · History · Edit