题目链接https://leetcode.com/problems/move-zeroes/

题目内容:

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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

题目翻译

给定一个数组,写一个函数,将数组中的0都移动到数组末尾,同时保持非零数字的顺序。
比如,数组nums=[0, 1, 0, 3, 12],调用你的函数后,数组变成[1, 3, 12, 0, 0]
注意:
1. 你必须在原数组上进行修改,不能拷贝一个新数组;
2. 尽量减少你的操作次数。

设置两个指针fast, slow。快指针fast遍历,遇到非零数传回慢指针(slow)。如果快慢指针指向的不是同一个元素,则快指针指向的元素归零,然后慢指针向后移动一个格,

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        for(int fast=0,slow=0;fast<nums.size();fast++){        //快指针fast遍历,遇到非零数传回慢指针(slow)。如果快慢指针指向的不是同一个元素,则快指针指向的元素归零,然后慢指针向后移动一个格,
            if(nums[fast]!=0){                      
                if(slow!=fast){                      //
                    nums[slow]=nums[fast];
                    nums[fast]=0;
                }
                 slow++;
                }
        }
        
    }
};

leetcode 283. Move Zeroes -easy的更多相关文章

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

  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. LeetCode 283 Move Zeroes(移动全部的零元素)

    翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...

  4. [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 ...

  5. Java [Leetcode 283]Move Zeroes

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

  6. Leetcode 283 Move Zeroes python

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

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

  8. LeetCode 283 Move Zeroes 解题报告

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

  9. [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 ...

随机推荐

  1. kvm之四:从网上镜像安装虚拟机Centos6.8

    1.再加块硬盘,格式化挂载至新建目录/kvm2下 2.CentOS 6.8镜像地址 http://mirrors.163.com/centos/6.8/os/x86_64/ 3.配置安装参数,执行安装 ...

  2. vs2013和.net 4.5.1调用.net core中的Kestrel(基于libuv)的http服务器代码 两种方式

    nuget获取相关的包:两个:Microsoft.AspNetCore.Server.Kestrel 和 Microsoft.Extensions.Logging.Console 编译完成后手工将pa ...

  3. Kaggle竞赛 —— 房价预测 (House Prices)

    完整代码见kaggle kernel 或 Github 比赛页面:https://www.kaggle.com/c/house-prices-advanced-regression-technique ...

  4. Python下载图片小程序

    欢迎大侠们指正批评 思路: 1.引入相关的python文件(import re  import urllib) 2.读取对应网页的html文件(使用 urllib) def getHtml(url): ...

  5. Java虚拟机之Java内存区域

    Java虚拟机运行时数据区域 ⑴背景:对于c/c++来说程序员来说,需要经常去关心内存运行情况,但对于Java程序员,只需要在必要时关心内存运行情况,这是因为在Java虚拟机自动内存管理机制的帮助下, ...

  6. Beta第六天

    听说

  7. 转git取消commit

     如果不小心commit了一个不需要commit的文件,可以对其进行撤销. 先使用git log 查看 commit日志 commit 422bc088a7d6c5429f1d0760d008d8 ...

  8. MySQL 服务安装及命令使用

    MySQL 服务安装及命令使用 课程来源说明 本节实验后续至第17节实验为本课程的进阶篇,都基于 MySQL 官方参考手册制作,并根据实验楼环境进行测试调整改编.在此感谢 MySQL 的开发者,官方文 ...

  9. Android实验报告

    实验名称:Android程序设计 实验时间:2017.5.24 实验人员:20162309邢天岳(结对同学20162313苑洪铭) 实验目的:使用android stuidio开发工具进行基本安卓软件 ...

  10. 201421123042 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...