移动零
题目大意
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
解题思路一
使用双指针
- 确定数组元素,输入数组
- 对数组元素进行移动
移动的思路:
①设两个索引index1,index2,
② index1指向当前数组下标元素(表示移动后非0元素的数组下标),index2作为遍历数组时的指向下标
③下标都从0开始,
1)若index2==0,则index1继续指向当前位置,index2++
2)若index2!=0,则将index2指向元素移动到index1指向的位置
【即index2一直往后找,找到一个非0元素就往前移,因此结束循环时index1正好等于非0元素的个数, index2正好等于数组元素的个数】 - 对数组进行输出
代码实现
import java.util.Scanner;
/*
移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*/
public class MoveZero{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数组nums的大小:");
int num = input.nextInt();
int[] nums = new int[num];
System.out.print("请输入" + num + "个整数:");
for (int i = 0; i < nums.length; i++) { // 通过循环输入num个整数,构成数组
nums[i] = input.nextInt();
}
/*
* 移动的思路: 设两个索引index1,index2,
* index1指向当前数组下标元素(表示移动后非0元素的数组下标),index2作为遍历数组时的指向下标
* 下标都从0开始,①若index2==0,则index1继续指向当前位置,index2++
* ②若index2!=0,则将index2指向元素移动到index1指向的位置
* (即index2一直往后找,找到一个非0元素就往前移,因此结束循环时index1正好等于非0元素的个数, index2正好等于数组元素的个数)
*/
int index1 = 0;// 默认都指向第一个元素
int index2 = 0;
while (index2 != num) {// 当索引没有指向数组最后一位元素
if (nums[index2] != 0) {// 如果该位置元素不为0,则移动到index1指向的位置
nums[index1] = nums[index2];
index1++;// 两个索引都指向下一个位置
index2++;
} else {// 如果index2指向为0,则index2继续后移
index2++;
}
}
for (int i = index1; i < index2; i++) {// 剩下的元素即index2-index1,也就是0的个数
nums[i] = 0;
}
for (int i = 0; i < index1 - 1; i++) {// 通过冒泡对非0元素进行排序
for (int j = 0; j < index1 - 1 - i; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
// 打印输出
System.out.print("移动零后的数组为:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
/*
* 也可以这样输出
*
* 输出非0元素
* for (int i = 0; i < index1; i++){
* System.out.print(nums[i] + " ");
* }
* 输出0
* for (int i = index1; i < index2; i++) {
* System.out.print(nums[i] + " ");
* }
*/
}
}
解题思路二
- 确定数组元素,输入数组
- 通过排序 使得数组有序
- 通过循环 将0元素移动到最后
移动的思路:
由于数组已经排好序,因此0一定是在最前面的,每一次移动时查看第一个元素,如果是0,则后面的元素依次往前移动一次,将0(即此时的第一个元素)移动到数组的最后。
当第一个元素不为0时,移动完成。(由于之前已经排好序,因此第一个元素不为0时数组已经完成移动) - 打印输出
代码实现
import java.util.Scanner;
/*
移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
*/
public class MoveZero {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入数组nums的大小:");
int num = input.nextInt();
int[] nums = new int[num];
System.out.print("请输入" + num + "个整数:");
for (int i = 0; i < nums.length; i++) { // 通过循环输入num个整数,构成数组
nums[i] = input.nextInt();
}
// 以上部分与解题思路一一致
for (int i = 0; i < nums.length - 1; i++) {// 通过冒泡排序使数组有序,易知0在最前面
for (int j = 0; j < nums.length - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
// 通过循环来移动0,由于不确定0的个数(即循环次数),因此用while循环
while (true) {
int temp = nums[0];// 0最小,因此排序后在最前面,也由此可知当第一个元素不为0时移动完毕
if (temp == 0) {// 如果第一个元素为0,则通过循环将0后面的元素按序一次前移
for (int j = 0; j < nums.length - 1; j++) {
nums[j] = nums[j + 1];
}
nums[nums.length - 1] = temp;// 将0移到最后
} else {
break;// 由于数组有序,因此当第一个元素为非0时,数组顺序已经符合题意,跳出循环
}
}
System.out.print("移动零后的数组为:");
for (int i = 0; i < nums.length; i++) {
System.out.print(nums[i] + " ");
}
}
}
运行结果
两种方法的运行结果是一样的
- Demo1
- Demo2