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 = ;
    int nonzero_bit = ;
    while(nonzero_bit<numsSize)
    {
    if(nums[nonzero_bit] != )
    {
    if(nonzero_bit != zero_bit)
    {
    nums[zero_bit++] = nums[nonzero_bit];
    nums[nonzero_bit] = ;
    }else{
    ++zero_bit;
    }
    }
    ++nonzero_bit;
    }
    }

LeetCode283:Move Zeros的更多相关文章

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

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

  2. Leetcode Move Zeros

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

  3. LeetCode 283 Move Zeros

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

  4. Leetcode-283 Move Zeroes

    #283.   Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mai ...

  5. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  6. leetcode-easy-array-283 move zeros

    mycode  77.24% class Solution(object): def moveZeroes(self, nums): """ :type nums: Li ...

  7. [刷题] 283 Move Zeros

    要求 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 实现 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostr ...

  8. LeetCode 283

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

  9. LeetCode Animation 题目图解汇总(持续更新中...)

    我会尽力将LeetCode上所有的题目都用动画的形式演示出来,期待与你见证这一天! GitHub Repo:LeetCode Animation Follow: MisterBooo · GitHub ...

随机推荐

  1. UVa 10020 (最小区间覆盖) Minimal coverage

    题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...

  2. HDU 1247 Hat’s Words (字符串匹配,暴力)

    题意: 给出一堆单词,如果有一个单词可以分成左右串两个单词,并且在所给的一堆单词中存在,就是hat词,统计所有这样的词,并按字典序输出. 思路: 注意定义,一个hat词可以被两部分已经存在的词组成,那 ...

  3. Javaweb里面的filter,listener,servlet

    Filter 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装 ...

  4. Oracle 中的 TO_DATE 和 TO_CHAR 函数

    Oracle 中的 TO_DATE 和 TO_CHAR 函数oracle 中 TO_DATE 函数的时间格式,以 2008-09-10 23:45:56 为例 格式 说明 显示值 备注 Year(年) ...

  5. oracle interval-partition 解决range分区大难题

    博客<oracle分区>中讲了oracle的几种分区,并且对于oracle的典型分区如Range分区和List分区给了示例. 在实际运用Range分区时,遇到了这样的难题: createt ...

  6. linux 开机自动启动脚本方法

    通过现场对这次天津iptv demo项目的调测.对iptv这套系统有了更好的认识和理解.由于iptv本身需要安装许多服务.而现场实施中有没有把这些需要启动服务的脚本加入到开 机自动运行中.如果服务器重 ...

  7. C#中的选择查询相关

    看代码实现: using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...

  8. SQL语句构建器类

    问题 Java程序员面对的最痛苦的事情之一就是在Java代码中嵌入SQL语句.这么来做通常是由于SQL语句需要动态来生成-否则可以将它们放到外部文件或者存储过程中.正如你已经看到的那样,MyBatis ...

  9. [C#搜片神器] 之P2P中DHT网络爬虫原理

    继续接着上一篇写:使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)[搜片神器] 昨天由于开源的时候没有注意运行环境,直接没有考虑下载BT种子文件时生成子文件夹,可能导致有的朋友运行 ...

  10. Android 应用启动速度优化

    现在很多的应用一开始点击的时候总会出现黑屏或者白屏,甚至前段时间微信也有同样的问题.其实白屏或者黑屏还是一些其他的东西,都是因为Android 主题的问题,只要自己自定义一个启动主题,问题完美解决. ...