当前位置:网站首页>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;
}
}
边栏推荐
- [network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
- Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- JVM_常见【面试题】
- POJ 3207 Ikki&#39; s Story IV – Panda&#39; s Trick (2-SAT)
- Example of shutter text component
- Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
- mod_wsgi + pymssql通路SQL Server座
- 范式的数据库具体解释
- [Yann Lecun likes the red stone neural network made by minecraft]
猜你喜欢

Continuous test (CT) practical experience sharing

【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】

In simple terms, interview surprise Edition

Ideas and methods of system and application monitoring

腾讯T3大牛手把手教你,大厂内部资料

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

PowerPivot - DAX (first time)

Cesium 点击绘制圆形(动态绘制圆形)

New generation garbage collector ZGC

Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
随机推荐
Problems encountered in using RT thread component fish
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
redisson bug分析
mod_wsgi + pymssql通路SQL Server座
Digital triangle model acwing 1018 Minimum toll
持续测试(CT)实战经验分享
Error analysis ~csdn rebound shell error
String length limit?
Case ① | host security construction: best practice of 3 levels and 11 capabilities
02 基础入门-数据包拓展
广州首个数据安全峰会将在白云区开幕
B-杰哥的树(状压树形dp)
Appx代码签名指南
方法关键字Deprecated,ExternalProcName,Final,ForceGenerate
Poj1149 pigs [maximum flow]
腾讯T3大牛手把手教你,大厂内部资料
Anaconda安裝後Jupyter launch 沒反應&網頁打開運行沒執行
New generation garbage collector ZGC
POJ 3207 Ikki&#39;s Story IV – Panda&#39;s Trick (2-SAT)
SSH connection denied