LeetCode:Move Zeroes

【 Problem reproduction 】

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

【 High quality algorithms 】

public class Solution {
public void moveZeroes(int[] nums) {
int index=0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=0)
nums[index++]=nums[i];
}
while(index<nums.length)
nums[index++]=0;
}
}

【 Reflection after the question 】

There are also Application of double pointer ,index It's a slow pointer ,i Represents a fast pointer ,i Keep moving back , And judge whether the element in the position is 0, If 0 Words , Just move back ; If not for 0 Words , Give Way index Equal to this value , This is the most critical step , ad locum index To update the array from scratch , Put all the non 0 The number is updated into the array , Neglected 0 It's likely to be given a new value in the update . So the element is 0 Moving back doesn't affect the algorithm .

LeetCode:Move Zeroes More articles about

  1. [LeetCode] Move Zeroes Move zero

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  2. LeetCode——Move Zeroes

    Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...

  3. LeetCode Move Zeroes ( Simple questions )

    The question : Given an integer array nums, Ask for all of them 0 Move to the end , And maintain all non 0 The relative position of integers remains unchanged . Ideas : Sweep it over , Two pointers maintain 0 And non 0 At the junction of , Will not 0 It's just a forward assignment . C++ class Solu ...

  4. leetcode:283. Move Zeroes(Java) answer

    Reprint please indicate the source :z_zhaojun The blog of Original address :http://blog.csdn.net/u012975705/article/details/50493772 Title address :https://leet ...

  5. leetcode The journey (7)-Move Zeroes

    Move Zeroes Title Description : Given an array nums, write a function to move all 0's to the end of it while maint ...

  6. LeetCode Javascript Realization 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  7. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution First move the non-zero elements to the front of the array , The rest can be filled with zeros . class Solution { public: void moveZeroes(vect ...

  8. 【leetcode】Move Zeroes

    Move Zeroes subject : Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

  9. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

Random recommendation

  1. SQL Server Aggregate function algorithm optimization skills

    Sql server Aggregate function is widely used in practical work to meet various requirements , The optimization of aggregate function has naturally become a focus , Whether a program is optimized or not directly determines the declaration cycle of the program .Sql server Aggregate functions for a set of values ...

  2. fight to win or die Windows 10 (8) - Control UI: StateTrigger

    [ Source download ] fight to win or die Windows 10 (8) - Control UI: StateTrigger author :webabcd Introduce the battle behind the back Windows 10 And Control UI VisualState And ...

  3. [ turn ]ASP.NET MVC 3 Razor + jqGrid Example

    In this paper, from :http://www.cnblogs.com/think8848/archive/2011/07/15/2107828.html I wrote an article about jqGrid The article , Because it's a company project ...

  4. WinServer2008R2 Deploy .NET4.0 Program matters needing attention

    Deployment considerations :   1.IIS Application pool Integration mode    2. stay web.config Medium system.webServer Next , add to <modules runAllManagedModulesForA ...

  5. javaScript Dynamic parameters

    javaScript It's dynamic language , So the dynamic parameters are also innate , I'm going to pick up javaScript The parameters are Arguments This attribute , selection <script type="text/java ...

  6. WindowsForm Instantiate the control with a string name

    private Control FindControl(Control control, string controlName) { Control c1; foreach (Control c in ...

  7. Switch view Animation

    Code : #import "MainViewController.h" @interface MainViewController () @end @implementation M ...

  8. oracle Full text search notes

    1. Delete the lexical parser exec ctx_ddl.drop_preference('my_lexer'); 2. Create a Chinese lexical parser exec ctx_ddl.create_preference ('my_ ...

  9. bat How to create a folder automatically ( Name it after the current time )

    First, check the current date and time directly :( perhaps cmd Directly input date,time see ) @echo off color 0a set dt=%date%%time% echo %dt% pause 1. Use ...

  10. ServerSocket It's super simple HTTP The server

    1. Introduction to relevant knowledge HTTP agreement HTTP Is one of the common application layer protocols , It's a text oriented protocol .HTTP Message transmission is based on TCP agreement ,TCP The protocol consists of header and data part , and HTTP It is contained in TCP The data part of the protocol , Here's the picture HTT ...