当前位置:网站首页>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.
边栏推荐
- Redis启动时提示Creating Server TCP listening socket *:6379: bind: No error
- To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
- [Server data recovery] Data recovery case of offline multiple disks in mdisk group of server Raid5 array
- shell脚本专题(07):文件由cfs到bos
- 硬件大熊原创合集(2022/07更新)
- 在表格数据上,为什么基于树的模型仍然优于深度学习?
- 【周赛复盘】LeetCode第304场单周赛
- [Neural Network] This article will take you to easily analyze the neural network (with an example of spoofing your girlfriend)
- modbus bus module DAM-8082
- Fuzzy query in Map pass-by-value and object pass-by-value
猜你喜欢
shell脚本专题(07):文件由cfs到bos
首篇 NLP 领域图神经网络综述:127 页,从图构建到实际应用面面观
Prometheus's Recording rules practice
文库网站建设源码分享
Try compiling QT test on Allwinner V853 development board
PanGu-Coder:函数级的代码生成模型
在全志V853开发板试编译QT测试
从普通进阶成优秀的测试/开发程序员,一路过关斩将
SENSORO成长伙伴计划 x 怀柔黑马科技加速实验室丨以品牌力打造To B企业影响力
Screen: GFF, OGS, Oncell, Incell of full lamination process
随机推荐
AntDB database appeared in the 24th high-speed exhibition, helping smart high-speed innovative applications
GZIPOutputStream 类源码分析
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
Zabbix6.0钉钉机器人告警
How to install voice pack in Win11?Win11 Voice Pack Installation Tutorial
Use of message template placeholders
对于web性能优化我有话说!
Goldfish Brother RHCA Memoirs: CL210 manages OPENSTACK network -- network configuration options
力扣刷题之求两数之和
C#/VB.NET Extract table from PDF
The XML configuration
重保特辑|筑牢第一道防线,云防火墙攻防演练最佳实践
app直播源码,点击搜索栏自动弹出下拉框
安装GBase 8c数据库的时候,报错显示“Resource:gbase8c already in use”,这怎么处理呢?
Industry Salon Phase II丨How to enable chemical companies to reduce costs and increase efficiency through supply chain digital business collaboration?
用VS2013编译带boost库程序时提示 fatal error C1001: 编译器中发生内部错误
A simple Flask PIN
想随时、随地、随心使用数据库的朋友们,全体注意!
explain each field introduction
选择合适的 DevOps 工具,从理解 DevOps 开始