当前位置:网站首页>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.
边栏推荐
- Try compiling QT test on Allwinner V853 development board
- 在GBase 8c数据库后台,使用什么样的命令来对gtm、dn节点进行主备切换的操作?
- vtk体绘制代码报错的解决办法(代码在vtk7,8,9中都能运行),以及VTK数据集网站
- The function realization of the national standard GB28181 protocol EasyGBS platform compatible with the old version of the inflow port
- GBase 8c中怎么查询数据库配置参数,例如datestyle。使用什么函数或者语法呢?
- 三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.
- Prometheus的Recording rules实践
- Tencent Cloud Hosting Security x Lightweight Application Server | Powerful Joint Hosting Security Pratt & Whitney Version Released
- Redis的内存淘汰策略和过期删除策略的区别是什么
- How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
猜你喜欢
随机推荐
When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
Prometheus的Recording rules实践
Source code analysis of GZIPOutputStream class
从普通进阶成优秀的测试/开发程序员,一路过关斩将
DAO开发教程【WEB3.0】
文库网站建设源码分享
LeetCode 1374. Generate an odd number of each character string
消息模板占位符的使用
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
ExcelPatternTool: Excel表格-数据库互导工具
Zabbix6.0 DingTalk robot alarm
explain 各字段介绍
安装GBase 8c数据库的时候,报错显示“Resource:gbase8c already in use”,这怎么处理呢?
【木棉花】#夏日挑战赛# 鸿蒙小游戏项目——数独Sudoku(3)
MySQL database - stored procedures and functions
MySQL中超键、主键及候选键的区别是什么
在Map传值与对象传值中模糊查询
The XML configuration
有点奇怪!访问目的网址,主机能容器却不行
MySQL数据库————流程控制









