# 283.move-zeroes

## Statement

• Difficulty: Easy
• Tag: `数组` `双指针`

``````输入: nums = [0,1,0,3,12]

``````

``````输入: nums = [0]

• `1 <= nums.length <= 104`
• `-231 <= nums[i] <= 231 - 1`

• Difficulty: Easy
• Tag: `Array` `Two Pointers`

Given an integer array `nums`, move all `0`'s to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Example 1:

``````Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
``````

Example 2:

``````Input: nums = [0]
Output: [0]
``````

Constraints:

• `1 <= nums.length <= 104`
• `-231 <= nums[i] <= 231 - 1`

Follow up: Could you minimize the total number of operations done?

## Solution

``````#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define endl "\n"
#define fi first
#define se second
#define all(x) begin(x), end(x)
#define rall rbegin(a), rend(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define complete_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define mst(x, a) memset(x, a, sizeof(x))
#define MP make_pair

using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
using VLL = std::vector<ll>;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;

template <typename T, typename S>
inline bool chmax(T &a, const S &b) {
return a < b ? a = b, 1 : 0;
}

template <typename T, typename S>
inline bool chmin(T &a, const S &b) {
return a > b ? a = b, 1 : 0;
}

#ifdef LOCAL
#include <debug.hpp>
#else
#define dbg(...)
#endif

class Solution {
public:
void moveZeroes(vector<int> &nums) {
int i = 0;
for (const auto &a : nums) {
if (a != 0) {
nums[i++] = a;
}
}
while (i < nums.size()) {
nums[i++] = 0;
}
}
};

#ifdef LOCAL

int main() {
return 0;
}

#endif
``````