All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0201_0300.s0283_move_zeroes.Solution.dart Maven / Gradle / Ivy

The newest version!
// #Easy #Top_100_Liked_Questions #Array #Two_Pointers #Algorithm_I_Day_3_Two_Pointers
// #Programming_Skills_I_Day_6_Array #Udemy_Arrays #Big_O_Time_O(n)_Space_O(1)
// #2024_10_09_Time_344_ms_(93.12%)_Space_155.6_MB_(50.26%)

class Solution {
  void moveZeroes(List nums) {
    int firstZero = 0;
    for (int i = 0; i < nums.length; i++) {
      if (nums[i] != 0) {
        _swap(firstZero, i, nums);
        firstZero++;
      }
    }
  }

  void _swap(int index1, int index2, List numbers) {
    int temp = numbers[index2];
    numbers[index2] = numbers[index1];
    numbers[index1] = temp;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy