#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
    void moveZeroes(vector<int>& nums)  {
        // 将0当作是空,遍历这个数组,遇到非空元素,直接写入空位置
        int i = 0;
        for (int j = 0; j < nums.size(); ++j) {
            if (nums[j]) swap(nums[i++], nums[j]);
        }
    }
};

int main() {
    // vector<int> nums = {0, 1, 0, 3, 12};
    vector<int> nums = {0, 0};
    Solution().moveZeroes(nums);
    for (auto i : nums) cout << i << " ";
    cout << endl;
    return 0;
}

本站由 Rizxfrog 使用 Stellar 创建。