void moveZeroes(int* nums, int numsSize) {int last_non_zero_found_at = 0;for (int i = 0; i < numsSize; i++) {if (nums[i] != 0) {// 交换 nums[last_non_zero_found_at] 和 nums[i]int temp = nums[last_non_zero_found_at];nums[last_non_zero_found_at] = nums[i];nums[i] = temp;last_non_zero_found_at++;}}
}
class Solution:def moveZeroes(self, nums: List[int]) -> None:"""Do not return anything, modify nums in-place instead."""# 使用双指针法last_non_zero_found_at = 0# 移动所有非零元素到数组的前面部分for i in range(len(nums)):if nums[i] != 0:nums[last_non_zero_found_at] = nums[i]last_non_zero_found_at += 1# 将剩余的位置填充为零for i in range(last_non_zero_found_at, len(nums)):nums[i] = 0