当前位置:网站首页>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.
边栏推荐
- Win11如何删除升级包?Win11删除升级包的方法
- LeetCode 0152. 乘积最大子数组:dp + 原地滚动
- C#/VB.NET:从 PDF 文档中提取所有表格
- explain each field introduction
- 【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件
- No need to crack, install Visual Studio 2013 Community Edition on the official website
- GZIPOutputStream 类源码分析
- 重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
- Flowable-based upp (unified process platform) running performance optimization
- C#/VB.NET 从PDF中提取表格
猜你喜欢

The XML configuration

Prometheus的Recording rules实践

#yyds dry goods inventory# Interview must brush TOP101: the last k nodes in the linked list

【pyqt5】自定义控件 实现能够保持长宽比地缩放子控件

DAO开发教程【WEB3.0】

Flowable-based upp (unified process platform) running performance optimization

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

明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度

三种方案解决:npm WARN config global --global, --local are deprecated. Use --location=global instead.

【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
随机推荐
SQL function TO_DATE (2)
明尼苏达大学团队结合高通量实验与机器学习,实现有效可预测的特定位点重组过程,可调节基因编辑速度
mysql函数的作用有哪些
LeetCode 0152. 乘积最大子数组:dp + 原地滚动
Map传值
What are the application advantages of SaaS management system?How to efficiently improve the digital and intelligent development level of food manufacturing industry?
The life cycle and scope
Use of message template placeholders
使用常见问题解答软件的好处有哪些?
【蓝桥杯选拔赛真题47】Scratch潜艇游戏 少儿编程scratch蓝桥杯选拔赛真题讲解
123123123123
MLX90640 Infrared Thermal Imager Temperature Measurement Module Development Notes (Complete)
cf:D. Magical Array【数学直觉 + 前缀和的和】
DAO development tutorial [WEB3.0]
深入浅出Flask PIN
哈哈!一个 print 函数,还挺会玩啊!
1065 A+B and C (64bit)
C#/VB.NET: extracted from the PDF document all form
升哲科技携全域数字化方案亮相2022全球数字经济大会
通配符 SSL/TLS 证书