LeetCode 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].
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相关推荐
- Leetcode PHP题解--D68 283. Move Zeroes
2019独角兽企业重金招聘Python工程师标准>>> D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元 ...
- LeetCode之283. Move Zeroes
---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...
- 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 ...
- 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 ...
- 283. Move Zeroes(数组篇)
//Java方法一 class Solution {public void moveZeroes(int[] nums) {if(nums==null) {return;//表示没有输出}//定义两个 ...
- 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)); 解 ...
- [东哥的leetcode刷题日记] leetcode 283 : Move Zeroes
leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: ...
- [LeetCode] Move Zeroes - 整数数组处理问题
目录: 1.Move Zeroes - 数组0移到末尾 [顺序交换] 2. 一.Move Zeroes 题目概述: Given an arraynums, write a function to m ...
- C#LeetCode刷题之#283-移动零(Move Zeroes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3907 访问. 给定一个数组 nums,编写一个函数将所有 0 移 ...
最新文章
- windows oracle path,Oracle-Windows平台Path的问题
- 李飞飞团队从动物身上get AI新思路,提出RL计算框架
- 图像处理之直方图均衡MATLAB代码实现
- 在C++ Builder中用socket api来写网络通讯程序(同时支持TCP和UDP协议)
- Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]
- JeecgBoot 常见问题QA
- iphone储存空间系统怎么清理_iPhone 储存空间里的“系统”项占用过多怎么办?...
- 默认地址路径修改_修改Docker默认路径
- 纯小白成功安装交叉编译工具arm-none-eabi-gcc
- LeetCode刷题——26. 删除排序数组中的重复项
- i4助手linux,爱思i4助手刷机教程
- matlab做线性规划图
- 汇编in和out介绍
- 《动手学深度学习》入门环境安装
- 时间序列(数据分析)
- 如何解决Unity高版本使用EasyTouch5的问题
- 员工不明白面试流程,傻傻等了三个月,收到面试评价短信蒙了
- UICollectionView教程:重用、选择和排序
- 艾伦图灵_艾伦唤醒控制
- IDEA SpringBoot 同一个模块启动多个服务(实例)