当前位置:网站首页>Leetcode question brushing record | 283_ Move zero
Leetcode question brushing record | 283_ Move zero
2022-07-08 02:09:00 【coder_ sure】
leetcode Record of writing questions |283 _ Move zero
author github link : github link
Force to buckle 283 topic
type : Array question
subject :
Given an array nums
, Write a function that will 0
Move to end of array , While maintaining the relative order of non-zero elements .
Please note that , You must operate on the array in place without copying it .
Example 1
Input : nums = [0,1,0,3,12]
Output : [1,3,12,0,0]
Example 2
Input : nums = [0]
Output : [0]
Their thinking
Train of thought reminder : Double pointer ( Fast pointer and slow pointer )
Train of thought details :
- Define two pointers , for example fast and slow
- Place both pointers at the beginning of the array , That is to say, make
fast = 0,slow = 0
- The fast pointer traverses the entire array in turn ,
When to take action ?
- When the fast pointer encounters
It's not equal to 0
When the number of , Take action at this time : Point the slow pointer to the value of the position and the fast pointer to the value of the positionIn exchange for
- After switching positions , Move the position of the slow pointer to the next position +1
- repeat
4~5
Until the traversal is complete
c++ edition
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size();
int fast = 0,slow = 0;
for(int i = 0;i<n;i++){
if(nums[i]!=0){
swap(nums[fast], nums[slow]);
slow++;
}
fast++;
}
}
};
python edition
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
""" Do not return anything, modify nums in-place instead. """
fast=0
slow=0
for i in range(0,len(nums)):
if nums[i]!=0:
nums[fast],nums[slow] = nums[slow],nums[fast]
slow = slow+1
fast = fast+1
边栏推荐
- 牛熊周期与加密的未来如何演变?看看红杉资本怎么说
- In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
- VIM use
- Introduction à l'outil nmap et aux commandes communes
- Nacos microservice gateway component +swagger2 interface generation
- Nmap tool introduction and common commands
- 喜欢测特曼的阿洛
- 《通信软件开发与应用》课程结业报告
- Anan's judgment
- EMQX 5.0 发布:单集群支持 1 亿 MQTT 连接的开源物联网消息服务器
猜你喜欢
Alo who likes TestMan
很多小伙伴不太了解ORM框架的底层原理,这不,冰河带你10分钟手撸一个极简版ORM框架(赶快收藏吧)
[knowledge map] interpretable recommendation based on knowledge map through deep reinforcement learning
线程死锁——死锁产生的条件
MQTT X Newsletter 2022-06 | v1.8.0 发布,新增 MQTT CLI 和 MQTT WebSocket 工具
Application of slip ring in direct drive motor rotor
[recommendation system paper reading] recommendation simulation user feedback based on Reinforcement Learning
C语言-Cmake-CMakeLists.txt教程
《ClickHouse原理解析与应用实践》读书笔记(7)
COMSOL --- construction of micro resistance beam model --- final temperature distribution and deformation --- addition of materials
随机推荐
Beaucoup d'enfants ne savent pas grand - chose sur le principe sous - jacent du cadre orm, non, ice River vous emmène 10 minutes à la main "un cadre orm minimaliste" (collectionnez - le maintenant)
Applet running under the framework of fluent 3.0
burpsuite
云原生应用开发之 gRPC 入门
Talk about the realization of authority control and transaction record function of SAP system
MQTT X Newsletter 2022-06 | v1.8.0 发布,新增 MQTT CLI 和 MQTT WebSocket 工具
Ml backward propagation
#797div3 A---C
What are the types of system tests? Let me introduce them to you
ArrayList源码深度剖析,从最基本的扩容原理,到魔幻的迭代器和fast-fail机制,你想要的这都有!!!
[target tracking] |dimp: learning discriminative model prediction for tracking
关于TXE和TC标志位的小知识
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
The circuit is shown in the figure, r1=2k Ω, r2=2k Ω, r3=4k Ω, rf=4k Ω. Find the expression of the relationship between output and input.
burpsuite
Reading notes of Clickhouse principle analysis and Application Practice (7)
Kwai applet guaranteed payment PHP source code packaging
For friends who are not fat at all, nature tells you the reason: it is a genetic mutation
Redisson distributed lock unlocking exception
Talk about the cloud deployment of local projects created by SAP IRPA studio