题目链接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. 真是没想到,ikvm.net居然停止开发了。

    看样子作者对.net已经失去了信心 http://weblog.ikvm.net/CommentView.aspx?guid=33ea525f-a291-418a-bd6a-abdf22d0662b# ...

  2. Vim配置及使用技巧

    要说Linux下比较好用的文本编辑器,我推荐vim(当然很多人都用emacs,可我没用过),用vim也有一年左右,有些心得体会想与诸位分享.在我的学习过程中,借鉴了不少优秀的博客,其中有csdn大神n ...

  3. 201621123062《java程序设计》第十周作业总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 2. 书面作业 本次PTA作业题集异常 2.1. 常用异常 结合题集题目7-1回答 2.1.1 自己以前 ...

  4. python学习笔记-问题

    1.字典按照值进行排序输出 2.返回函数-闭包的使用

  5. 为label或者textView添加placeHolder

    Tip:使用textView的代理需要在头文件中加入: <UITextViewDelegate> h文件 @interface FeedbackViewController : UIVie ...

  6. java的socket通信

    本文讲解如何用java实现网络通信,是一个非常简单的例子,我比较喜欢能够立马看到结果,所以先上代码再讲解具体细节. 服务端: import java.io.BufferedReader; import ...

  7. 第四篇:用IntelliJ IDEA 搭建基于jersey的RESTful api

    编译器:Intellij IDEA 系统环境: MAC OS 相关技术:Maven.tomcat 7.jdk8 1.创建项目 首先创建一个web Application项目(这里我们打算用maven引 ...

  8. 织梦cms/dedecms清理冗余废弃未引用图片方法

    原理描述: 在原有织梦后台菜单中增加"清理冗余图片按钮",实现清理冗余图片的功能. 操作步骤: 1. 打开后台dede\sys_sql_query.php代码 在该文件中搜索如下代 ...

  9. .Net Core MongoDB 简单操作。

    一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...

  10. JAVA_SE基础——42.final修饰符

    高手勿喷~ final关键字可用于修饰类.变量和方法,它有"这是无法改变的"或者"最终"的含义,因此被final修饰的类.变量和方法将具有以下特征: 1.fin ...