给定一个数组 nums
,编写一个函数将所有 0
移动到数组的末尾,同时保持非零元素的相对顺序。
输入: [0,1,0,3,12] 输出: [1,3,12,0,0]
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
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 j in i..nums.len() {
nums[j] = 0;
}
}
}
impl Solution {
pub fn move_zeroes(nums: &mut Vec<i32>) {
let mut i = 0;
for _ in 0..nums.len() {
if nums[i] == 0 {
nums.remove(i);
nums.push(0);
} else {
i += 1;
}
}
}
}