给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。
例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, 12, 0, 0]。
注意事项:
    必须在原数组上操作,不要为一个新数组分配额外空间。
    尽量减少操作总数。
详见:https://leetcode.com/problems/move-zeroes/description/

Java实现:

  1. class Solution {
  2. public void moveZeroes(int[] nums) {
  3. for(int i=0,j=0;i<nums.length;++i){
  4. if(nums[i]!=0){
  5. swap(nums,i,j++);
  6. }
  7. }
  8. }
  9. private void swap(int[] nums,int i,int j){
  10. int tmp=nums[i];
  11. nums[i]=nums[j];
  12. nums[j]=tmp;
  13. }
  14. }

C++实现:

  1. class Solution {
  2. public:
  3. void moveZeroes(vector<int>& nums) {
  4. for(int i=0,j=0;i<nums.size();++i)
  5. {
  6. if(nums[i])
  7. {
  8. swap(nums[i],nums[j++]);
  9. }
  10. }
  11. }
  12. };

参考:https://www.cnblogs.com/grandyang/p/4822732.html

283 Move Zeroes 移动零的更多相关文章

  1. [LeetCode] 283. Move Zeroes 移动零

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

  2. [leetcode]283. Move Zeroes移零

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

  3. 283. Move Zeroes把零放在最后面

    [抄题]: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  4. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  5. 283. Move Zeroes(C++)

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

  6. LeetCode Javascript实现 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. 283. Move Zeroes【easy】

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

  8. 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 ...

  9. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

随机推荐

  1. Balanced Binary Tree (二叉树DFS)

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  2. 洛谷 P1018 乘积最大

    P1018 乘积最大 题目描述 今年是国际数学联盟确定的“ 20002000 ――世界数学年”,又恰逢我国著名数学家华罗庚先生诞辰 9090 周年.在华罗庚先生的家乡江苏金坛,组织了一场别开生面的数学 ...

  3. 采用jmeter测试dubbo服务接口

    http://www.kissyu.org/2017/02/08/jmeter%E6%B5%8B%E8%AF%95dubbo%E6%8E%A5%E5%8F%A3/

  4. PopupMenu的演示样例

    弹出菜单是停靠在一个View上的一个模式菜单. 假设View对象下方有空间,那么弹出菜单将显示在停靠对象的下方,否则会显示在上方. 这是很实用的: 源代码地址:http://download.csdn ...

  5. 戴尔PowerEdge服务器RAID控制卡的配置

    示例演示环境:PowerEdge R620 + H710p Raid控制卡  + 9 x 300G 10k SAS 硬盘 H310.H710.H810的配置方法与H710P大致相同,在此不再累述. 特 ...

  6. JQuery编程demo练习

    JQuery练习demo:     编敲代码,实现:     1.选中当中一列的复选框时,该复选框所在行的背景色高亮显示(黄色). 2.取消选中复选框时,所在行的背景色恢复. ============ ...

  7. TCP/IP常见问题总结(二)

    上一篇的传送门:TCP/IP常见问题总结(一) 6. TCP滑动窗体与回退N帧协议 TCP作为一个提供可靠服务的传输层协议,对于数据的发送必须拥有一套良好的反馈机制.让发送方得知接收方接收到了数据.而 ...

  8. python 字符编码处理问题总结 彻底击碎乱码!

    Python中常常遇到这种字符编码问题,尤其在处理网页源代码时(特别是爬虫中): UnicodeDecodeError: 'XXX' codec can't decode bytes in posit ...

  9. [计算机故障]toshiba笔记本计算机无法正常启动,每次均需要按ESC

    同事一台toshiba的笔记本计算机,无法正常启动,每次均需要按ESC才可以正常后续动作. 之后系统可以正常工作. 排查过程: 1.尝试恢复bios到默认配置:不行,而且不小心搞了个蓝屏,还好记得是“ ...

  10. SQL SERVER 安装软件 及导入项目流程

    1.安装sqlsever2000及以上 数据库 (在百度上找安装文档) 创建账户 密码 2.解压SQL2000-KB884525-SP4-x86-CHS.EXE补丁 之后安装补丁 ,在安装补丁是会用到 ...