当前位置:网站首页>Leetcode question 283 Move zero
Leetcode question 283 Move zero
2022-07-06 20:14:00 【Invite the wind to intoxicate the moon】
283. Move zero
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]
Tips :
1 <= nums.length <= 104
-231 <= nums[i] <= 231 - 1
Advanced : Can you minimize the number of operations completed ?
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/move-zeroes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Answer key : Double pointer ( Moving mode )
We define two pointers i,j, All traverse from the beginning ,i Go back first , When subscript is i The value of is not 0 when , Move the value to j Position of subscript , after j+1.j Recorded non 0 Number . Then subscript from j It has been assigned as 0

class Solution {
public void moveZeroes(int[] nums) {
if(nums==null){
return ;
}
// The first time you traverse ,j Pointer record non 0 The number of , As long as the time is not 0 All of are given to nums[j]
int j=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=0){
nums[j++]=nums[i];
}
}
// Not 0 Element statistics are finished , Next, put j And the array after it is assigned 0 that will do
for(int i=j;i<nums.length;i++){
nums[i]=0;
}
}
}
The time complexity is O(2n), That is to say O(n)
Answer key : Double pointer ( In exchange for )
It's also a double pointer i,j, Point back from the beginning , But when you encounter non 0 Elements , We'll exchange
class Solution {
public void moveZeroes(int[] nums) {
int j=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=0){
swap(nums,i,j);
j++;
}
}
}
public void swap(int [] nums,int i,int j ){
int temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
}
}
边栏推荐
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- 新一代垃圾回收器—ZGC
- BeagleBoneBlack 上手记
- Color is converted to tristimulus value (r/g/b) (dry stock)
- AsyncHandler
- 小微企业难做账?智能代账小工具快用起来
- rt-thread i2c 使用教程
- 5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
- Wonderful coding [hexadecimal conversion]
- [cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
猜你喜欢

Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
腾讯T2大牛亲自讲解,跳槽薪资翻倍
![[network planning] Chapter 3 data link layer (3) channel division medium access control](/img/df/dd84508dfa2449c31d72c808c50dc0.png)
[network planning] Chapter 3 data link layer (3) channel division medium access control

22-07-05 upload of qiniu cloud storage pictures and user avatars

持续测试(CT)实战经验分享

Digital triangle model acwing 1018 Minimum toll

报错分析~csdn反弹shell报错

深度学习分类网络 -- ZFNet

Vscode debug run fluent message: there is no extension for debugging yaml. Should we find yaml extensions in the market?
腾讯安卓开发面试,android开发的基础知识
随机推荐
微信小程序常用集合
Example of shutter text component
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
Cesium 两点之间的直线距离
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
报错分析~csdn反弹shell报错
Introduction to enterprise lean management system
[cloud lesson] EI lesson 47 Mrs offline data analysis - processing OBS data through Flink
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
Guangzhou's first data security summit will open in Baiyun District
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
使用ssh连接被拒
An East SMS login resurrection installation and deployment tutorial
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
2022年6月语音合成(TTS)和语音识别(ASR)论文月报
Poj3617 best cow line
Tencent T3 Daniel will teach you hand-in-hand, the internal information of the factory
Discussion on beegfs high availability mode
【云原生与5G】微服务加持5G核心网
redisson bug分析