Move Zeroes

Move all zeroes in an array to the end while maintaining the order of non-zero elements.

Solution

#include <vector>
using namespace std;
 
class Solution {
public:
  void moveZeroes(vector<int> &nums) {
    int i = 0;
    for (int j = 0; j < nums.size(); j++) {
        if(nums[j] != 0){
            nums[i] = nums[j];
            i++;
        }
    }
    for(;i<nums.size();i++){
        nums[i] = 0;
    }
  }
};