当前位置:网站首页>Mobile Zero of Likou Brush Questions
Mobile Zero of Likou Brush Questions
2022-08-01 19:06:00 【Lanzhou Qianfan】
The mobile zero of the force button brush question
This is the number one in the quiz283题.
题目
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序.
请注意 ,必须在不复制数组的情况下原地对数组进行操作.

The question asks to move the zeros in the array to the back of the array.And can only operate on the original array,不可以复制,You cannot shuffle the order of the original array elements.
So for the movement of the elements,If the brain rotation is not very smart,It is recommended to draw a picture yourself to try it out,See how to move.
You see how these scrambled zeros move to the back

Move zero to the back,Then you need to move the non-zero elements to the front.
一种想法,我们可以用两个指针,Then start to initialize the head of the array pointing to the same time.
And how to move it?The yellow pointer starts to move,移动到3,它不是零,We replace it with the white one0的值.
继续,到这里的时候,Our yellow pointer points to zero,We certainly don't move forward zero.
There is no movement here,Do not move the white pointer,The yellow ones continue to move.
移动到4的时候,Then the number at the yellow point replaces the value at the white execution.
然后两个指针继续移动,The value at the yellow pointer now replaces the value at the white pointer again,The yellow pointer reaches the end
Then we continue to move the white pointer to the end point,and assigns the value at the following index0.
This moves it.
We write code according to this logic
class Solution {
public void moveZeroes(int[] nums) {
int pre01 =0;
for (int pre02 = 0; pre02 < nums.length; pre02++) {
if (nums[pre02]!=0)
{
nums[pre01++]=nums[pre02];
}
}
for (int i = pre01; i < nums.length; i++) {
nums[i]=0;
}
}
}

还有一种方法,There is a difference in thinking.This time we initialize two pointers.
Only this time around,我们是这样做的.

We exchange directly when comparing.
然后移动,Both are zeros here,不交换.yellow move,White unchanged
Swap next
继续
Compare carefully,There is a difference between the two methods.Our exchange patterns are different,The position to which the pointer moves is different.
来看代码实现.
int pre01 =0;
for (int pre02 = 0; pre02 < nums.length; pre02++) {
if(nums[pre01]==0&&nums[pre02]!=0)
{
int n= nums[pre02];
nums[pre02]=nums[pre01];
nums[pre01++] =n;
}
else if (nums[pre01]!=0)
{
pre01++;
}
}
This execution efficiency is not as high as our above method,But the last one seems to be more like a loophole.
边栏推荐
- LeetCode 0151. Reverse a string of words
- How to query database configuration parameters in GBase 8c, such as datestyle.What function or syntax to use?
- SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
- MLX90640 Infrared Thermal Imager Temperature Measurement Module Development Notes (Complete)
- 首篇 NLP 领域图神经网络综述:127 页,从图构建到实际应用面面观
- 无需破解,官网安装Visual Studio 2013社区版
- The XML configuration
- odoo+物联网
- 金鱼哥RHCA回忆录:CL210管理OPENSTACK网络--网络配置选项
- TestNG multiple xml for automated testing
猜你喜欢

暑假第二周总结博客

Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法

小白系统初始化配置资源失败怎么办

MySQL数据库————存储过程和函数

Source code analysis of GZIPOutputStream class

Shell script topic (07): file from cfs to bos

#yyds干货盘点# 面试必刷TOP101: 链表中倒数最后k个结点

【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
如何看待腾讯云数据库负责人林晓斌借了一个亿炒股?

Hardware Bear Original Collection (Updated 2022/07)
随机推荐
MLX90640 红外热成像仪测温模块开发笔记(完整篇)
In the background of the GBase 8c database, what command is used to perform the master-slave switchover operation for the gtm and dn nodes?
[Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
odoo 编码规范(编程规范、编码指南)
Win11校园网无法连接怎么办?Win11连接不到校园网的解决方法
【综述专栏】IJCAI 2022 | 图结构学习最新综述:研究进展与未来展望
请你说说多线程
Redis启动时提示Creating Server TCP listening socket *:6379: bind: No error
modbus bus module DAM-8082
How to query database configuration parameters in GBase 8c, such as datestyle.What function or syntax to use?
How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
Friends who want to use the database anytime, anywhere and as you like, all attention!
DAO development tutorial [WEB3.0]
[Neural Network] This article will take you to easily analyze the neural network (with an example of spoofing your girlfriend)
What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
无需破解,官网安装Visual Studio 2013社区版
深入浅出Flask PIN
Redis的内存淘汰策略和过期删除策略的区别是什么
Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server