题号:283
给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,而维持其他所有非0元素的相对位置。

解法一:把非零的数放到数组前,然后后面的位置设置为0
java
/**
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
* 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
*/
public class Test01_283_001 {
public static void main(String[] args) {
moveZeroes(new int[]{0, 0, 1, 0, 0, 0, 3, 12});
}
public static void moveZeroes(int[] nums) {
// 将非零数据往前移动
int index = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) {
continue;
}
nums[index] = nums[i];
index++;
}
// 后面的数据都设置为0
for (int i = index; i < nums.length; i++) {
nums[i] = 0;
}
System.out.println(Arrays.toString(nums));
}
}
解法二:使用两个指针
java
/**
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
* 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
*/
public class Test01_283_2 {
public static void main(String[] args) {
moveZeroes(new int[]{1});
}
public static void moveZeroes(int[] nums) {
// 使用left、right两个指针,left不为0,right为left+1
// 首先找到left为0的位置
int left = 0;
while (left < nums.length) {
if (nums[left] == 0) {
break;
}
left++;
}
// left和right交换
for (int right = left + 1; right < nums.length; right++) {
if (nums[right] != 0 && left != right) {
int temp = nums[right];
nums[right] = nums[left];
nums[left] = temp;
left++;
}
}
System.out.println(Arrays.toString(nums));
}
}
双指针的另一种写法:
java
/**
* 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
* 请注意 ,必须在不复制数组的情况下原地对数组进行操作。
*/
public class Test01_283_002 {
public static void main(String[] args) {
moveZeroes(new int[]{5, 0, 1, 0, 0, 0, 3, 12});
}
public static void moveZeroes(int[] nums) {
int left = 0;
int right = 0;
while (right < nums.length) {
if (nums[right] == 0) {
right++;
} else if (nums[right] != 0 && left < right) {
ArrayUtils.swap(nums, left, right);
left++;
right++;
} else { // nums[right] != 0 && left == right
left++;
right++;
}
}
System.out.println(Arrays.toString(nums));
}
}