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.

Notice
  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
 
Example

Given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

LeetCode上的原题,请参见我之前的博客Move Zeroes

解法一:

class Solution {
public:
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
void moveZeroes(vector<int>& nums) {
for (int i = , j = ; i < nums.size(); ++i) {
if (nums[i]) {
swap(nums[i], nums[j++]);
}
}
}
};

解法二:

class Solution {
public:
/**
* @param nums an integer array
* @return nothing, do this in-place
*/
void moveZeroes(vector<int>& nums) {
int left = , right = ;
while (right < nums.size()) {
if (nums[right]) {
swap(nums[left++], nums[right]);
}
++right;
}
}
};

[LintCode] Move Zeroes 移动零的更多相关文章

  1. [LeetCode] 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. [LintCode] Trailing Zeroes 末尾零的个数

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  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. 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. 283 Move Zeroes 移动零

    给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序.例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, ...

  7. leetcode之旅(7)-Move Zeroes

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

  8. 【leetcode】283. Move Zeroes

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

  9. LeetCode:Move Zeroes

    LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...

随机推荐

  1. APP设计尺寸规范大全,APP界面设计新手教程【官方版】(转)

    正值25学堂一周年之际,同时站长和APP设计同仁们在群里(APP界面设计 UI设计交流群,APP界面设计⑥群 APPUI设计③群58946771 APP设计资源⑤群 386032923欢迎大家加入交流 ...

  2. Chrome浏览器之 Postman 安装

    Postman 是一款发送 HTTP 请求的 Chrome 插件.开发后端程序的同学可以用它来测试自己写的应用程序是否能够正常访问. 现在由于国内的网络限制, Chrome 浏览器里无法访问“扩展程序 ...

  3. JSON语法简介 介绍 json

    JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation),类似 XML,但比 XML 更小.更快,更易解析. 实例 { "employees ...

  4. HTTP 请求方式: GET和POST的比较(转)

    GET和POST是HTTP的两个常用方法.   什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 ...

  5. 《DSP using MATLAB》示例Example4.13

    代码: b = [1, 0, -1]; a = [1, 0, -0.81]; % [R, p, C] = residuez(b,a); Mp = (abs(p))' Ap = (angle(p))'/ ...

  6. DOM--3 DOM核心和DOM2 HTML(1)

    网页是一种结构化的文档,使用一组预定义的XML和HTML标签进行标记:当浏览器接受到网页文档时,会根据文档类型和关联的样式表对其进行解析,然后以可视化形式显示在屏幕上. DOM是一组用来描述脚本怎样与 ...

  7. Swift3.0语言教程获取C字符串

    Swift3.0语言教程获取C字符串 Swift3.0语言教程获取C字符串,为了让Swift和C语言可以实现很好的交互,开发者可以使用NSString的cString(using:)方法在指定编码格式 ...

  8. jvm 监控

    jvm监控可视化的有 jconsole .jmc .jvisualvm 其中jvisualvm开启一些监控会导致他自己关闭. 并且jdk下有很多工具可以进行jvm监控, jmap -histo:liv ...

  9. iris数据集

    iris以鸢尾花的特征作为数据来源,数据集包含150个数据集,分为3类,每类50个数据,每个数据包含4个属性,是在数据挖掘.数据分类中非常常用的测试集.训练集. 链接地址

  10. sqlserver中创建链接服务器

    链接服务器在跨数据库/跨服务器查询时非常有用(比如分布式数据库系统中),本文将以图文方式详细说明如何利用SQL Server Management Studio在图形界面下创建链接服务器.     1 ...