Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
我想着先不是零的都移到前面,然后后面的补零。
public void moveZeroes(int[] nums) {
int j =

这道题目要求在不复制数组的情况下,将所有零元素移动到数组末尾,同时保持非零元素的相对顺序。提供两种解题思路:一是先将非零元素移到前面,再填充零;二是边移动边填充零,力求操作次数最小化。
订阅专栏 解锁全文
3659

被折叠的 条评论
为什么被折叠?



