-
Notifications
You must be signed in to change notification settings - Fork 0
/
283.移动零.rs
48 lines (46 loc) · 963 Bytes
/
283.移动零.rs
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=rust
*
* [283] 移动零
*
* https://leetcode-cn.com/problems/move-zeroes/description/
*
* algorithms
* Easy (63.94%)
* Likes: 1276
* Dislikes: 0
* Total Accepted: 514.3K
* Total Submissions: 804.3K
* Testcase Example: '[0,1,0,3,12]'
*
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*
* 示例:
*
* 输入: [0,1,0,3,12]
* 输出: [1,3,12,0,0]
*
* 说明:
*
*
* 必须在原数组上操作,不能拷贝额外的数组。
* 尽量减少操作次数。
*
*
*/
// @lc code=start
impl Solution {
pub fn move_zeroes(nums: &mut Vec<i32>) {
let mut i = 0;
for j in 0..nums.len() {
if nums[j] != 0 {
nums[i] = nums[j];
i += 1;
}
}
for k in i..nums.len() {
nums[k] = 0;
}
}
}
// @lc code=end