Skip to content

Instantly share code, notes, and snippets.

@Fly-Style
Last active April 9, 2019 08:26
Show Gist options
  • Select an option

  • Save Fly-Style/ef40f8ed0ec53288644689012fd547b2 to your computer and use it in GitHub Desktop.

Select an option

Save Fly-Style/ef40f8ed0ec53288644689012fd547b2 to your computer and use it in GitHub Desktop.
// zeroes to right
void move_zeroes(vector<int> &arr) {
int i = 0;
for(auto el : arr) {
if (el != 0) {
arr[i++] = el;
}
}
for(int k = i; k < arr.size())
arr[k] = 0;
}
// zeroes to left
void move_zeroes(vector<int> &arr) {
int i = arr.size() - 1;
for(int el = arr.size(); el >= 0; el--) {
if (arr[el] != 0)
arr[i--] = arr[el];
}
for(int k = i; k >= 0; --k)
arr[k] = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment