当前位置:网站首页>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;
}
}
边栏推荐
- JS implementation force deduction 71 question simplified path
- Discussion on beegfs high availability mode
- Transformer model (pytorch code explanation)
- Linear distance between two points of cesium
- Introduction of Xia Zhigang
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- 【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
- 02 基础入门-数据包拓展
猜你喜欢
Example of applying fonts to flutter
腾讯T3手把手教你,真的太香了
PowerPivot——DAX(初识)
Configuration and simple usage of the EXE backdoor generation tool quasar
持续测试(CT)实战经验分享
Continuous test (CT) practical experience sharing
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Cesium 点击绘制圆形(动态绘制圆形)
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
Tencent T2 Daniel explained in person and doubled his job hopping salary
随机推荐
【GET-4】
RT thread I2C tutorial
Catch ball game 1
Tencent T4 architect, Android interview Foundation
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
Technology sharing | packet capturing analysis TCP protocol
B-杰哥的树(状压树形dp)
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
Method keywords deprecated, externalprocname, final, forcegenerate
Groovy basic syntax collation
腾讯云数据库公有云市场稳居TOP 2!
颜色(color)转换为三刺激值(r/g/b)(干股)
深度学习分类网络 -- ZFNet
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
PHP与EXCEL PHPExcel
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
[Yann Lecun likes the red stone neural network made by minecraft]
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
BeagleBoneBlack 上手记
小微企业难做账?智能代账小工具快用起来