当前位置:网站首页>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;
}
}
边栏推荐
- RT thread I2C tutorial
- Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
- Initial experience of addresssanitizer Technology
- Is it difficult for small and micro enterprises to make accounts? Smart accounting gadget quick to use
- Node. Js: express + MySQL realizes registration, login and identity authentication
- 精彩编码 【进制转换】
- Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
- [cloud native and 5g] micro services support 5g core network
- Catch ball game 1
- BUUCTF---Reverse---easyre
猜你喜欢
![[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
Tencent T2 Daniel explained in person and doubled his job hopping salary
Tencent Android development interview, basic knowledge of Android Development

Deep learning classification network -- zfnet

A5000 vGPU显示模式切换

redisson bug分析

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅

Example of applying fonts to flutter

5. 無線體內納米網:十大“可行嗎?”問題
腾讯架构师首发,2022Android面试笔试总结
随机推荐
Introduction to enterprise lean management system
使用ssh连接被拒
深度学习分类网络 -- ZFNet
小微企业难做账?智能代账小工具快用起来
爬虫(14) - Scrapy-Redis分布式爬虫(1) | 详解
RT-Thread 组件 FinSH 使用时遇到的问题
Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
【计网】第三章 数据链路层(3)信道划分介质访问控制
Appx code signing Guide
22-07-05 upload of qiniu cloud storage pictures and user avatars
Deep learning classification network -- zfnet
Speech recognition (ASR) paper selection: talcs: an open source Mandarin English code switching corps and a speech
HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
Cesium 点击绘制圆形(动态绘制圆形)
Special topic of rotor position estimation of permanent magnet synchronous motor -- Summary of position estimation of fundamental wave model
5. Nano - Net in wireless body: Top 10 "is it possible?" Questions
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
AddressSanitizer 技术初体验
Wechat applet common collection
Tencent T4 architect, Android interview Foundation