当前位置:网站首页>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;
}
}
边栏推荐
- 精彩编码 【进制转换】
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- PowerPivot——DAX(初识)
- Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
- 2022年6月语音合成(TTS)和语音识别(ASR)论文月报
- Groovy基础语法整理
- js实现力扣71题简化路径
- 夏志刚介绍
- The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
- HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
猜你喜欢
持续测试(CT)实战经验分享
Enumeration gets values based on parameters
BUUCTF---Reverse---easyre
某东短信登录复活 安装部署教程
PowerPivot - DAX (first time)
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
数字三角形模型 AcWing 1018. 最低通行费
Notes on beagleboneblack
Deep learning classification network -- zfnet
[network planning] Chapter 3 data link layer (3) channel division medium access control
随机推荐
[cloud native and 5g] micro services support 5g core network
Continuous test (CT) practical experience sharing
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
Classic 100 questions of algorithm interview, the latest career planning of Android programmers
持续测试(CT)实战经验分享
Pay attention to the partners on the recruitment website of fishing! The monitoring system may have set you as "high risk of leaving"
颜色(color)转换为三刺激值(r/g/b)(干股)
新一代垃圾回收器—ZGC
Database specific interpretation of paradigm
Tencent byte Alibaba Xiaomi jd.com offer got a soft hand, and the teacher said it was great
枚举根据参数获取值
A5000 vgpu display mode switching
22-07-05 upload of qiniu cloud storage pictures and user avatars
转让malloc()该功能后,发生了什么事内核?附malloc()和free()实现源
报错分析~csdn反弹shell报错
POJ 3207 Ikki&#39; s Story IV – Panda&#39; s Trick (2-SAT)
What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
RT thread I2C tutorial
小微企业难做账?智能代账小工具快用起来