Processing math: 100%
[Codewars] Moving Zeros To The End

Moving Zeros To The End

  • Time : O(n)
  • Space : O(n)
c++
1
2
3
4
5
6
7
8
9
#include <vector>

std::vector<int> move_zeroes(const std::vector<int>& input) {
std::vector<int> res(input.size());
for(int i = 0, p = 0; i < input.size(); i++) {
if(input[i]) res[p++] = input[i];
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/01/PS/Codewars/moving-zeros-to-the-end/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Related Issues not found

Please contact @SongHayoung to initialize the comment