题目:
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 i n-place without making a copy of the array.
2.Minimize the total number of operations.

思路:
循环判断nums.size()次,若nums[j]为0,将0移动到后面,并删除当前的0;如果不是0,就往后移一位。

代码:

class Solution {
public:void moveZeroes(vector<int>& nums) {int j=0;//因为要将0移动到后面,并删除当前的0,所以有时候索引不会变for(size_t i=0;i<nums.size();i++){//循环判断nums.size()次if(nums[j]==0){//如果当前是0,索引j不变nums.insert(nums.end(),0);//将0移动到后面,并删除当前的0nums.erase(nums.begin()+j);}else{//如果不是0,就往后移一位j++;}}}
};

LeetCode 283. Move Zeroes相关推荐

  1. Leetcode PHP题解--D68 283. Move Zeroes

    2019独角兽企业重金招聘Python工程师标准>>> D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元 ...

  2. LeetCode之283. Move Zeroes

    ---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...

  3. Leet Code OJ 283. Move Zeroes [Difficulty: Easy]

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  4. LeetCode之Move Zeroes

    1.题目 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  5. 283. Move Zeroes(数组篇)

    //Java方法一 class Solution {public void moveZeroes(int[] nums) {if(nums==null) {return;//表示没有输出}//定义两个 ...

  6. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    int arr[] = {0,1,0,3,12}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解 ...

  7. [东哥的leetcode刷题日记] leetcode 283 : Move Zeroes

    leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: ...

  8. [LeetCode] Move Zeroes - 整数数组处理问题

    目录: 1.Move Zeroes  - 数组0移到末尾 [顺序交换] 2. 一.Move Zeroes 题目概述: Given an arraynums, write a function to m ...

  9. C#LeetCode刷题之#283-移动零(Move Zeroes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3907 访问. 给定一个数组 nums,编写一个函数将所有 0 移 ...

最新文章

  1. windows oracle path,Oracle-Windows平台Path的问题
  2. 李飞飞团队从动物身上get AI新思路,提出RL计算框架
  3. 图像处理之直方图均衡MATLAB代码实现
  4. 在C++ Builder中用socket api来写网络通讯程序(同时支持TCP和UDP协议)
  5. Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]
  6. JeecgBoot 常见问题QA
  7. iphone储存空间系统怎么清理_iPhone 储存空间里的“系统”项占用过多怎么办?...
  8. 默认地址路径修改_修改Docker默认路径
  9. 纯小白成功安装交叉编译工具arm-none-eabi-gcc
  10. LeetCode刷题——26. 删除排序数组中的重复项
  11. i4助手linux,爱思i4助手刷机教程
  12. matlab做线性规划图
  13. 汇编in和out介绍
  14. 《动手学深度学习》入门环境安装
  15. 时间序列(数据分析)
  16. 如何解决Unity高版本使用EasyTouch5的问题
  17. 员工不明白面试流程,傻傻等了三个月,收到面试评价短信蒙了
  18. UICollectionView教程:重用、选择和排序
  19. 艾伦图灵_艾伦唤醒控制
  20. IDEA SpringBoot 同一个模块启动多个服务(实例)

热门文章

  1. 笨方法“学习python笔记之元组(tuple)
  2. 默认参数,命名参数,可变参数
  3. win10缺少 `VCRUNTIME140.dll` 文件(解决篇)
  4. 孜然导航系统 v2.3
  5. matlab trapz二重积分函数_matlab二重积分
  6. 苹果CMSV10橙色自适应25影视网主题模板
  7. 网站随机动态密码代码
  8. 蓝天采集器自动化采集发布
  9. Typecho双栏博客免费主题—Splity
  10. 综合短网址缩短和还原源码