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. C#生成图形验证码

    先看效果: 再上代码 public class CaptchaHelper { private static Random rand = new Random(); private static in ...

  2. BZOJ3028: 食物

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3028 题解:列出母函数乘起来化简之后再展开,用插板法即可. 代码: #include<c ...

  3. PHP中括号“{}”的3个作用

    1,将多个独立语句合并为一个复合语句,例如“if .... else ....”中推荐如此使用. 2,在变量的间接引用中进行定界,避免歧义.例如“${$my_var[8]}”与“${$my_var}[ ...

  4. ecshop 在首页每个商品下显示已销售数量

    1.在includes/lib_goods.php文件末尾加入以下代码 function get_buy_sum($goods_id) { $sql = "select sum(goods_ ...

  5. java-过滤器-监听器-拦截器

    1.过滤器 Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是过滤字符编码.做一些业务逻辑判断等.其工作原理是,只要你在web.xml ...

  6. RAC 之 RMAN 恢复

    RAC 下的RMAN 讲究的是备份和还原的策略要一致.备份策略的不同,会导致备份结果的分步不同,进而影响恢复的策略和步骤.一般情况下,恢复策略和备份策略必须是对应的.如果备份策略进行了修改,那么恢复也 ...

  7. Windows下PHP+Eclipse开发环境搭建 及错误解决(apache2.2服务无法启动 发生服务特定错误:1)

    前言 Eclipse与php/apache的关系:Eclipse只是用来写代码的,如果想要在浏览器查看运行效果就要让php/apache的运行目录指向你的代码目录.Eclipse貌似不会自己和apac ...

  8. 定制Bootstrap遇到无法下载的解决——Blob下载注意事项

    今天定制bootstrap(在这里),全部的勾都选过了,于是兴高采烈地点击“编译并下载”.等了一会儿,迅雷7跳出来了“新建下载任务”,但是它居然说这个url不合法! url像这样: blob:http ...

  9. linux系统上Mysql数据库导入导出操作

    需求:把MySQL数据库目录中的dz数据库备份到/home/dz_bak.sql ,然后再新建一个数据库dzbak,最后把/home/dz_bak.sql 导入到数据库dzbak中.操作如下:以下操作 ...

  10. 为什么从PhoneGap中逃离

    我是一名移动应用的开发者,从JAVA 为主的Android到以Objective-C为主的iOS最后到以HTML5为主的跨平台开发,我已经走过了五年多的时光,而我也从一个底层的码农成长为项目负责人. ...