---
# System prepended metadata

title: 283. Move Zeroes
tags: [C++, LeetCode-Easy]

---

# 283. Move Zeroes
Difficulty: Easy

## Solution

```cpp=
/**
*** Author: R-CO
*** E-mail: daniel1820kobe@gmail.com
*** Date: 2020-11-05
**/
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

struct TestCaseStruct {
  vector<int> input;
  vector<int> expected_output;
};

class Solution {
 public:
  static void moveZeroes(vector<int> &nums) {
    vector<int>::iterator moved_position = nums.begin();
    for (auto &it : nums) {
      if (it != 0) {
        swap(*moved_position++, it);
      }
    }
  }
};

int main(int argc, char *argv[]) {
  vector<TestCaseStruct> test_cases = {{{1, 0, 2, 0, 3}, {1, 2, 3, 0, 0}}};

  size_t test_case_index = 0;
  for (auto &test_case : test_cases) {
    Solution::moveZeroes(test_case.input);
    auto is_pass = true;
    for (size_t i = 0; i < test_case.input.size(); ++i) {
      if (test_case.input[i] != test_case.expected_output[i]) {
        is_pass = false;
        break;
      }
    }

    if (is_pass) {
      cout << "Test case [" << test_case_index << "] is pass." << endl;
    } else {
      cout << "Test case [" << test_case_index << "] is fail." << endl;
    }
    ++test_case_index;
  }

  return EXIT_SUCCESS;
}

```

## Result

Success
Details 
Runtime: 4 ms, faster than 98.58% of C++ online submissions for Move Zeroes.
Memory Usage: 9.4 MB, less than 9.23% of C++ online submissions for Move Zeroes.

###### tags: `LeetCode-Easy` `C++`
