lc 283 Move Zeroes


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

analysation##

刚开始用了很蠢的方法,后来用STL很轻松就解决了!

solution1:蠢

void moveZeroes(vector<int> & nums) {
if (nums.size() <= 1) {
return;
} else {
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
if (nums[i] != 0) {
nums[sum] = nums[i];
sum++;
}
}
for (int i = sum; i < nums.size(); i++)
nums[i] = 0;
}
}

solution2:STL

int size = nums.size();
int pos = 0;
for (int i = 0; i < size; i++) {
if (nums[i] != 0) {
int temp = nums[i];
nums.erase(nums.begin() + i);
nums.insert(nums.begin() + pos, temp);
pos += 1;
}
}

LN : leetcode 283 Move Zeroes的更多相关文章

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

  2. leetcode 283. Move Zeroes -easy

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

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

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

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

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

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

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

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

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

随机推荐

  1. 1.spring boot要求最低jdk1.8,平安默认1.6问题,-》安装JDK1.8 2.maven 3.3.3要求最低jdk1.7->安装jdk 1.8

    1.spring boot要求最低jdk1.8,平安默认1.6问题,->安装JDK1.82.maven 3.3.3要求最低jdk1.7->安装jdk 1.8

  2. tomcat会自动解压webapps目录下的war包

    如图,把war包放到tomcat的webapps目录,会被自动解压

  3. yarn之安装依赖包

    安装依赖关系 yarn install用于安装项目的所有依赖项.依赖关系从您的项目package.json文件中检索,并存储在yarn.lock文件中. 开发包时,安装依赖关系最常见的是在 您刚刚检出 ...

  4. Python - 两个列表(list)组成字典(dict)

    使用zip函数, 把key和value的list组合在一起, 再转成字典(dict). 代码: # -*- coding: utf-8 -*- keys = ['a', 'b', 'c'] value ...

  5. 微信JSSDK使用指南

        这篇文章主要来说说微信JSSDK的一些配置和微信分享的使用,包括从前端sdk文件引入到server端和微信server的交互. 另外Tangide已经把一些工作做好了.我会简要说一说怎么把它用 ...

  6. HDMI信号解析

    参考资料:http://blog.sina.com.cn/s/blog_6cfd49b00102w00i.html: http://blog.csdn.net/gtkknd/article/detai ...

  7. JSON介绍及Android最全面解析方法(Gson、AS自带org.son、Jackson解析)

    前言 今天,我们来介绍一下现今主流的数据交换格式-JSON! 相同作为主流为数据交换格式-XML,假设有兴趣能够阅读我写的XML及其DOM.SAX.PULL解析方法和对照 文件夹 定义 JavaScr ...

  8. TMS320F28335项目开发记录6_28335之cmd文件具体解释

    1.CMD文件的作用 CMD文件的作用就像仓库的货物摆放记录一样,为程序代码和数据分配指定的空间. 2.C语言生成的段 C语言生成的段大致分为两大类:初始化和未初始化,已初始化的段含有真正的指令和数据 ...

  9. 2016/1/17 笔记 1,面向对象编程OOP 2,类 全

    面向对象编程OOP 编程方式的发展 1,面向过程 重用性低 维护工作量大  2,面向对象 重用性高 维护容易 概念 1,对象 Object 一个具体的事物 是类的实例      2,类Class 同一 ...

  10. APDU报文【转】

    本文转载自:http://www.cnbolgs.com/snail0404/p/5436348.html APDU   # APDU # 定义:APDU(ApplicationProtocolDat ...