LeetCode283:Move Zeros

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.

主要思路:保持两个指针,保证两个指针之间[zero_bit,nonzero_bit)范围内都是0,然后调换首个0和首个非零

  1. void moveZeroes(int* nums, int numsSize)
    {
        int zero_bit = 0;
        int nonzero_bit = 0;
       while(nonzero_bit<numsSize)
       {
           if(nums[nonzero_bit] != 0)
           {
               if(nonzero_bit != zero_bit)
               {
                   nums[zero_bit++] = nums[nonzero_bit];
                   nums[nonzero_bit] = 0;
               }else{
                   ++zero_bit;
               }
           }
           ++nonzero_bit;
       }
    }
时间: 2024-10-11 12:43:45

LeetCode283:Move Zeros的相关文章

[LeetCode283]Move Zeros将一个数组中为0的元素移至数组末尾

题目: 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:

LeetCode(283): Move Zeros

Move Zeros: 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].

【LeetCode】283. Move Zeros

题目 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:Yo

[Leetcode] Move Zeros

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: You

LeetCode283. Move ZeroesC语言

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: You 

[刷题] LeetCode 283 Move Zeros

要求: 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 class Solution{ 7 public: 8 void moveZeros(vector<int>& nums){ 9 vector<int> no

LeetCode:Move Zeroes - 将数组中的0移到最后

1.题目名称 Move Zeroes(将数组中的0移到最后) 2.题目地址 https://leetcode.com/problems/move-zeroes 3.题目内容 英文: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. 中文:给出一个数字数组,写一个函数将数组中所有的0移

LeetCode — (1)

摘要: Nim Game.WordPattern.Move zeros.First Bad version.Ugly Number五个算法的python实现. 一个月多没更新,大概是因为状态一直不太好吧,有几次打开却不知从何写起.总结一下这一个月多:看了几个算法:接触了hadoop虽然还不算会:会用linux:看了HTML,JS:拿了两个省奖,其中一个真是一直的梦想:加入了一个团队也算是离自己的梦想更近一步:去过自己喜欢的地方:吃吃吃玩玩玩:做了好几件自己喜欢的事:帮到了挺多人:此刻却突然纠结于

【Leetcode解题报告】

第1章  单链表 1.1  删除单链表中的结点 203 Remove Linked List Elements 83  Remove Duplicates from Sorted List 82  Remove Duplicates from Sorted List II 19  Remove Nth Node from End of List 237 Delete Node in a Linked List 1.2  单链表结点位置调整 206 Reverse Linked List 92