-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path283.移动零.cpp
More file actions
48 lines (46 loc) · 913 Bytes
/
283.移动零.cpp
File metadata and controls
48 lines (46 loc) · 913 Bytes
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
/*
* @lc app=leetcode.cn id=283 lang=cpp
*
* [283] 移动零
*
* https://leetcode-cn.com/problems/move-zeroes/description/
*
* algorithms
* Easy (64.02%)
* Likes: 1408
* Dislikes: 0
* Total Accepted: 604.6K
* Total Submissions: 944.5K
* Testcase Example: '[0,1,0,3,12]'
*
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*
* 示例:
*
* 输入: [0,1,0,3,12]
* 输出: [1,3,12,0,0]
*
* 说明:
*
*
* 必须在原数组上操作,不能拷贝额外的数组。
* 尽量减少操作次数。
*
*
*/
// @lc code=start
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int i=0;
for(auto x:nums) {
if(x) {
nums[i++]=x;
}
}
while(i<nums.size()) {
nums[i++]=0;
}
}
};
// @lc code=end