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. GDAL源码编译

    转自阿Fai, GDAL源码编译 在这里,我使用源码编译出C#可以使用的dll静态文件. 一.简单的编译 1.简单的认识 首先进入GDAL的源代码目录,可以看到有几个sln为后缀的文件名,比如make ...

  2. java编程思想-复用类

    /* 一个文件中只能有一个public类 并且此public类必须与文件名相同 */ class WaterSource { private String s; WaterSource() { Sys ...

  3. 实战恢复2950交换机的IOS

    本来想用两台交换机做实验的,可是通过console口进入其中一台交换机后却发现这个台交换器的IOS文件丢失了 本来正常进入交换机后应该是首先进入到用户模式的,而且提示符应该是">&qu ...

  4. CentOS 7加强安全性:

    CentOS 7加强安全性:1. 更改 root 密码************************************************************************* ...

  5. 图像处理之基础---用Shader实现的YUV到RGB转换:使用3重纹理实现 .

    上一篇中,我是用一个RGB格式的纹理来存储每一帧的画面,其中纹理为m_FrameWidth * m_FrameHeight大小,这样,在内存中,就必须要先对YUV的数据进行排序,然后才能当做RGB的数 ...

  6. Deferred Rendering(二)G-Buffer的组织

    先来看一张网上广为流传的<杀戮地带2>典型的Deferred Shading的G-Buffer组织: 这里补充解释下几个点: 不存Position,而由depth和屏幕像素坐标反推出来.參 ...

  7. unity3D游戏开发实战原创视频讲座系列13之帽子戏法游戏开发(预告)

    文件夹 第一讲  游戏演示项目创建 第二讲 游戏场景的编辑 第三讲  帽子的移动 第四讲  炮弹的产生 第六讲  游戏界面的完好 第七讲 各种UI的制作 第八讲  分数和爆炸特效   视持续更新中.. ...

  8. 解决ubuntu中firefox浏览器总是提示找不到server的问题

    这个情况在我机器上常常出现,并且时不时的给你出点问题.可是有些时候等一下就好了.或者把引擎换到百度的话它就又行得通了.. 被这个问题搞得非常烦.上网查了下说是防火墙啊之类的出问题.可是自己弄了后这个问 ...

  9. java jxl读取excel中Date类型

    Workbook book = Workbook.getWorkbook(excel); Sheet sheet = book.getSheet(0); int clos = sheet.getCol ...

  10. Android studio导入项目时的问题(Re-download dependencies and sync project (requires network))

    引入了别人的项目出现了这种情况提示是跟gradle cache有关,我的解决方法是跟gragle的配置有关 改下这个: distributionUrl=https\://services.gradle ...