#283.   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.

题解:维护两个下标,index和遍历数组的i

class Solution {
public:
void moveZeroes(vector<int>& nums) {
int index=;
for(int i=;i<nums.size();i++)
{
if(nums[i]!=)
{
swap(nums[index],nums[i]);
index++;
} }
}
};

Leetcode-283 Move Zeroes的更多相关文章

  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 -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

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

  10. LeetCode 283 Move Zeroes(移动全部的零元素)

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

随机推荐

  1. error C2065: 'INVALID_SET_FILE_POINTER' : undeclared identifier

    Searching MSDN for that constant brings up one result: it's a failure code for SetFilePointer() and ...

  2. Android学习之 Intent详解

    一. Intent 作用 Intent 是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由Intent来协助完成android各个组件之间的通讯.比如说调用startActivity()来 ...

  3. workerman 的属性

    <?php /** * Created by PhpStorm. * User: zeopean * Date: 2016-08-26 * Time: 16:35 */ use Workerma ...

  4. Asp.Net MVC4入门指南(3):添加一个视图

    在本节中,您需要修改HelloWorldController类,从而使用视图模板文件,干净优雅的封装生成返回到客户端浏览器HTML的过程. 您将创建一个视图模板文件,其中使用了ASP.NET MVC ...

  5. FMDB基本应用

    1.打开数据库 #import "ViewController.h" #import "FMDB.h" @interface ViewController () ...

  6. 【Java】XML解析之JDOM

    JDOM介绍 JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析.生成.序列化以及多种操作.使用jdom需要引入jdom.jar包. XML生成及解析 代码如下: pac ...

  7. [MySQL]load data local infile向MySQL数据库中导入数据时,无法导入和字段不分离问题。

    利用load data将文件中的数据导入数据库表中的时候,遇到了两个问题. 首先是load data命令无法执行的问题: 命令行下输入load data local infile "path ...

  8. @Html.Partials 加载分布视图传参数

    如何在视图中利用 viewData参数和model参数,示例如下 <body> <div style="background:#ffd800;width:200px;pad ...

  9. Mac mysql修改密码

    在网上看了很多的办法,其实解决办法都对,只是没有说明白: 1,首先启动mysql服务 2,mysqladmin -uroot -p 'newpassword' 此时需要输入登陆密码(数据库的密码,刚安 ...

  10. Oracle中改变表的Owner和tablespace

    初用Oracle,很多的不熟悉,建完库,没有建用户,也没创建表空间,就直接system用户建表添加数据,几个月过去,表建了近百个,数据添加了几万条,才越来越觉得这种方式缺点太多: 在PL/SQL中系统 ...