当前位置:网站首页>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;
}
}
边栏推荐
- Problems encountered in using RT thread component fish
- HDU 1026 search pruning problem within the labyrinth of Ignatius and the prince I
- Notes on beagleboneblack
- POJ 3207 Ikki&#39; s Story IV – Panda&#39; s Trick (2-SAT)
- JS implementation force deduction 71 question simplified path
- 5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
- 腾讯安卓开发面试,android开发的基础知识
- An East SMS login resurrection installation and deployment tutorial
- 青龙面板白屏一键修复
- String length limit?
猜你喜欢
【GET-4】
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
枚举根据参数获取值
RT thread I2C tutorial
Node.js: express + MySQL实现注册登录,身份认证
A5000 vGPU显示模式切换
Example of shutter text component
Cesium Click to draw a circle (dynamically draw a circle)
腾讯字节阿里小米京东大厂Offer拿到手软,老师讲的真棒
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
随机推荐
腾讯安卓开发面试,android开发的基础知识
BeagleBoneBlack 上手记
Wechat applet common collection
AsyncHandler
Monthly report of speech synthesis (TTS) and speech recognition (ASR) papers in June 2022
Initial experience of addresssanitizer Technology
Poj1149 pigs [maximum flow]
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
Groovy basic syntax collation
New generation garbage collector ZGC
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
枚举根据参数获取值
Problems encountered in using RT thread component fish
PHP与EXCEL PHPExcel
mod_wsgi + pymssql通路SQL Server座
Logstash expressway entrance
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
22-07-05 upload of qiniu cloud storage pictures and user avatars
Cesium 两点之间的直线距离
JVM_常见【面试题】