题目链接:
LeetCode - 283.移动零
题目分析:
题解代码:
#include<iostream>
#include<vector>
using namespace std;class Solution {
public:void moveZeroes(vector<int>& nums) {for (int cur = 0, dest = -1; cur < nums.size(); cur++) //设置初始条件{if (nums[cur] != 0) //处理非零元素{swap(nums[dest + 1], nums[cur]); //非零元素提到前面dest++;}}}
};int main()
{Solution so1; // 创建一个 Solution 对象vector<int> nums = { 0, 1, 0, 3, 12 }; // 定义一个 vector 并初始化so1.moveZeroes(nums);for (auto e : nums){cout << e << " ";}return 0;
}