当前位置:网站首页>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.
边栏推荐
- app直播源码,点击搜索栏自动弹出下拉框
- 小白系统初始化配置资源失败怎么办
- 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?
- SaaS管理系统的应用优势在哪里?如何高效提升食品制造业数智化发展水平?
- Selenium在远程中的截图
- Library website construction source code sharing
- Fuzzy query in Map pass-by-value and object pass-by-value
- Every calculation, & say what mean
- Summer vacation first week wrap-up blog
- SQL function TO_DATE (1)
猜你喜欢

基于flowable的upp(统一流程平台)运行性能优化

Win11怎么安装语音包?Win11语音包安装教程
![[pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio](/img/99/34f223614449fcee8e9322dff2e839.png)
[pyqt5] Custom controls to achieve scaling sub-controls that maintain the aspect ratio

C#/VB.NET:从 PDF 文档中提取所有表格

明日盛会|ApacheCon Asia 2022 Pulsar 技术议题一览

面试必问的HashCode技术内幕

The XML configuration

shell脚本专题(07):文件由cfs到bos

通配符 SSL/TLS 证书

Library website construction source code sharing
随机推荐
安徽建筑大学&杭州电子科技大学|基于机器学习方法的建筑可再生能源优化控制
Map传值
cf:D. Magical Array【数学直觉 + 前缀和的和】
SQL function TO_DATE (1)
When installing the GBase 8c database, the error message "Resource: gbase8c already in use" is displayed. How to deal with this?
Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
How to record and analyze your alchemy process - use notes of the visual artifact Wandb [1]
The XML configuration
英国伦敦大学|眼科强化学习:潜在应用和实施挑战
What should I do if the Win11 campus network cannot be connected?Win11 can't connect to campus network solution
123123123123
【木棉花】#夏日挑战赛# 鸿蒙小游戏项目——数独Sudoku(3)
力扣刷题之求两数之和
XML配置
Website construction process
Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
Three solutions: npm WARN config global --global, --local are deprecated. Use --location=global instead.
无需破解,官网安装Visual Studio 2013社区版
消息模板占位符的使用
Win11怎么安装语音包?Win11语音包安装教程