Processing math: 100%
[InterviewBit] Move Zeroes

Move Zeroes

  • Time : O(n)
  • Space : O(1)
c++
1
2
3
4
5
6
7
8
9
vector<int> Solution::solve(vector<int> &A) {
int p = 0;
for(int i = 0; i < A.size(); i++) {
if(A[i]) A[p++] = A[i];
}
while(p < A.size()) A[p++] = 0;
return A;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/14/PS/interviewbit/move-zeroes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Gitalking ...