-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode283.cpp
51 lines (47 loc) · 1.13 KB
/
leetcode283.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*************************************************
Author: wenhaofang
Date: 2023-03-21
Description: leetcode283 - Move Zeroes
*************************************************/
#include <bits/stdc++.h>
using namespace std;
/**
* 方法一:快慢指针
*
* 理论时间复杂度:O(n),其中 n 为数组大小
* 理论空间复杂度:O(1)
*/
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size();
if (n == 0) {
return;
}
int slow = -1;
int fast = -1;
while (true) {
slow++;
while (++fast < n && nums[fast] == 0);
if (fast < n) {
nums[slow] = nums[fast];
} else {
for (int i = slow; i < n; i++) {
nums[i] = 0;
}
break;
}
}
}
};
/**
* 测试
*/
int main() {
Solution* solution = new Solution();
vector<int> nums = {0, 1, 0, 3, 12};
solution -> moveZeroes(nums);
for(auto item: nums) {
cout << item << endl;
}
}