题目链接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. Matlab绘图基础——axis设置坐标轴取值范围

    peaks; axis tight  %Set the axis limits to equal the range of the data  axis square axis 'auto x'  % ...

  2. JavaScript(第三十一天)【JSON】

    前两章我们探讨了XML的结构化数据,但开发人员还是觉得这种微型的数据结构还是过于烦琐.冗长.为了解决这个问题,JSON的结构化数据出现了.JSON是JavaScript的一个严格的子集,利用JavaS ...

  3. C语言程序设计(基础)- 第0次作业

    亲爱的同学们,恭喜你成为一名大学生,我也很荣幸能够带大家一起学习大学的第一门专业基础课.还在军训的你,肯定对大学生活和计算机专业有着美好的憧憬,那么大学生活是什么样子的那?计算机专业应该怎么学习那?请 ...

  4. Alpha冲刺第十二天

    Alpha冲刺第十二天 站立式会议 项目进展 项目核心功能,如学生基本信息管理模块,学生信用信息模块,奖惩事务管理模块等等都已完成,测试工作大体结束. 问题困难 项目结束后对项目的阶段性总结缺乏一定的 ...

  5. scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250

    scrapy爬虫框架教程(二)-- 爬取豆瓣电影TOP250 前言 经过上一篇教程我们已经大致了解了Scrapy的基本情况,并写了一个简单的小demo.这次我会以爬取豆瓣电影TOP250为例进一步为大 ...

  6. android 检查软件是否有更新版本

    import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import com.yuxin.m ...

  7. python控制流 If-else

        控制流 If-else 我们处理现实生活中的问题时会做出决定,就像决定买哪种相机或者怎样更好的打篮球.同样我们写计算机程序的时候也要做相同的事情.我们通过 if-else 语句来做决定,我们使 ...

  8. Python多线程案例

    from time import ctime,sleep import threading def music(): for i in range(2): print ("I was lis ...

  9. Struts2之Action的实现

    对于Struts2框架来说,最重要的莫过于Action类的编写,类比于Servlet,Action类也是通过类的实例对象调用方法来处理请求的,Action类的实例对象是由Struts2的核心Filte ...

  10. BAT齐聚阿里安全-ASRC生态大会:呼吁联合共建网络安全白色产业链

    图说:近日,阿里安全-ASRC生态大会在杭州举行,包括BAT在内的20余家国内知名互联网企业代表,回顾过去一年网络安全面临的问题与挑战,共谋生态安全治理思路. "123456.111111. ...