当前位置:网站首页>LeetCode 283. 移动零(简单、数组)day12
LeetCode 283. 移动零(简单、数组)day12
2022-04-22 08:39:00 【White Code】
题目描述:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
请注意 ,必须在不复制数组的情况下原地对数组进行操作。
示例 1:
输入: nums = [0,1,0,3,12]
输出: [1,3,12,0,0]
示例 2:
输入: nums = [0]
输出: [0]
题目解析:
方法一:双指针法(自己当初想的方法)
1、遍历数组;
2、如果num[i] != 0 继续进行遍历,如果num[i]==0进行如下操作;
3、先设置两个指针,left_pointer和left_move。left_pointer指向当前数组元素为0的下标即为i,left_move指向其后一位元素下标;
4、从当前left_move开始判断当前元素是否为0,如果为零,left_move继续向后移动直到扎到第一个不为0的元素为止;
5、交换num[left_pointer ] 与 nums[left_move]的值 ,继续上述操作,直到循环结束
方法二:双指针方法(参考网上题解)
我们定义两个指针,i指针和k指针,i指针用来遍历整个nums数组,k指针用来放置nums数组元素。然后将非0元素按照原有的相对顺序都放置到nums数组前面,
剩下的位置都置为0。这样我们就完成了0元素的移动,同时也保持了非0元素的相对顺序。
具体过程如下:
1、定义两个指针i和k,初始化i = 0,k = 0。
2、i指针向后移动,遍整个nums数组,如果 nums[i] != 0,也就是说遇到了非0元素,此时我们就将nums[i]元素放置到nums[k]位置,同时k++后一位。
3、最后将k位置之后的元素都赋值为0。
作者:lin-shen-shi-jian-lu-k
链接:https://leetcode-cn.com/problems/move-zeroes/solution/283-yi-dong-ling-shuang-zhi-zhen-zuo-fa-6wmvs/
代码实现:
import java.util.Arrays;
public class MoveZeros {
public static void main(String[] args) {
int[] nums = {0, 1, 0,0, 3, 12, 15, 23, 0, 19, 0};
moveZeroes1(nums);
System.out.println(Arrays.toString(nums));
}
// 方法1: 双指针法1
public static void moveZeroes1(int[] nums) {
// 1. 如果数组为空或者数组的长度为1结束方法
if (nums == null || nums.length == 1) {
return;
}
// 2. 遍历数组进行查找
for (int i = 0; i < nums.length; i++) {
// 2.1 找到数组元素值为0的元素
if(nums[i] == 0){
// 2.2 声明两个变量
int left_pointer = i, left_move = i + 1;
while (left_move < nums.length && nums[left_move] == 0){
left_move++;
}
if(left_move >= nums.length){ // 后面的元素都为0可以退出了
break;
}else { // 交换元素
int temp = nums[left_move];
nums[left_move] = nums[left_pointer];
nums[left_pointer] = temp;
}
}
}
}
// 方法2: 双指针法2
public static void moveZeroes2(int[] nums){
// 1. 如果数组为空或者数组的长度为1结束方法
if (nums == null || nums.length == 1) {
return;
}
// 2. 设置双指针
int k = 0; // 记录数组中非0的个数
for(int i = 0; i < nums.length; i++){
if(nums[i] != 0){
nums[k++] = nums[i];
}
}
int k_zero = nums.length -k; // 记录数组中0的个数
for(int i = nums.length-1; k_zero != 0; i--){
nums[i] = 0;
k_zero--; // 每次k都要进行减一操作
}
}
}
版权声明
本文为[White Code]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_43751200/article/details/124325933
边栏推荐
- 824. Goat Latin (Analog)
- Leetcode0396. 旋转函数(medium,迭代)
- 单片机开发之裸机也能 “多任务”?
- ThreadLocal actual combat
- Using docker to build LNMP + redis development environment suitable for thinkphp5
- 解读智慧农业未来发展
- 精彩回顾|「源」来如此 第六期 - 开源经济与产业投资
- @ scheduled cron expression in quartz
- TypeScript(1)
- 2022 R1 quick opening pressure vessel operation exercises and online simulation examination
猜你喜欢
![C language to realize [shutdown program]](/img/b3/0364fda1bc27d754dd11eec055979d.jpg)
C language to realize [shutdown program]

RHCSA第一天作业

Scope and lifecycle of variables

Variable length array

【图像隐写】Fixed Neural Network Steganography: Train the images, not the network 整理

Intersecting linked list (set, double pointer)

Selection, sorting and optimization

關於我想借款卻被騙了一萬五這件事

On the difference between local variables and global variables

Flink流处理引擎系统学习(二)
随机推荐
Scope and lifecycle of variables
Breakthroughs and new development opportunities of smart watches
Advanced view of MySQL
Little known "three letter word"
Intersecting linked list (set, double pointer)
MySQL-高级-5 存储引擎
C bit starts the public beta
L2-004 搜索树判断
学习RHCSA的第二天
链表中倒数第k个节点(顺序查找、快慢指针)
宝宝起名神器小程序源码_支持多种流量主模式
健身房会员管理系统需求分析文档
2022 high voltage electrician test simulation 100 questions and answers
什么产品都还没有 马斯克的“无聊公司”估值已高达57亿美元
Eventbridge integrated cloud service practice
Cmake使用基础知识一之基础语法
Section I: the first step of portrait refinement - reasonable transfer
Return type of getchar function
1958年高考数学真题
QT file reading and writing practical tutorial