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. FJNUOJ1158(莫比乌斯反演)

    题目:给定n个数字a1...an.有m个询问,格式为L R X Y,意为求aL到aR之间与x的最大公因数为y的个数. 数据组数T<=20 1<=n,m<=1e5 1<=ai&l ...

  2. MongoDB小结19 - find【查询条件$all】

    利用all来查询所以满足的匹配项,已知数据库有这些数据 db.user.find({},{"_id":0}) { "fruit" : [ "apple ...

  3. openWrt 安装管理界面luci中文包

    openWrt15安装管理界面luci中文包   如果刚刷的openwrt15没有中文界面,用ssh连接路由后用opkg安装     root@bang-bang-tang:~# opkg insta ...

  4. 用vhd挂载并安装win7且建立分差vhd

    准备:硬盘分区激活第一个分区; imagex.exe; install.wim; winpe boot pc 1.cmd命令下,创建主vhd      (1)diskpart       (打开dis ...

  5. 基于MFC的一个简单计算器

    写一个简单的计算器并不是什么很难的事,主要目的是要通过这个程序来学习和分析其中的核心算法.这个简易计算器的核心部分就是对输入的表达式的正确性判断与求值,其中包括对表达式的解析.中缀表达式转后缀表达式. ...

  6. 解决myeclipse在mac中Retina屏幕中模糊的终极详细方法

  7. 关于Domain Sepcific Lang

    今天在看一些关于CO的东东 里面提到,用从语言派生出来的领域语言再去编写代码会大大加速开发进程 PHP应该是个典型的领域语言(Perl之于文本处理也是这样),虽然不是从什么其他领域派生出来的,但是使用 ...

  8. virtual (C# Reference)

    https://msdn.microsoft.com/en-us/library/9fkccyh4.aspx The virtual keyword is used to modify a metho ...

  9. go语言--time.After

    go语言--time.After https://blog.csdn.net/cyk2396/article/details/78873396 1.源码分析: // After waits for t ...

  10. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...