[LeetCode] 283. Move Zeroes
时间:2020-04-11
本文章向大家介绍[LeetCode] 283. Move Zeroes,主要包括[LeetCode] 283. Move Zeroes使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Description
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.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
Analyse
将一个数组里所有的0移到非0值得右边,不打乱非0值得顺序
关键在于in-place,不能申请额外的内存空间
遍历vector,用非0值覆盖0值就行了,用一个index记录当前写到vector的哪个位置了
void moveZeroes(vector<int>& nums) {
int len = nums.size();
int current_not_zero_index = 0;
int nonzero_count = 0;
for (int i = 0; i < len; i++) {
if (nums[i] == 0) {
continue;
} else {
++nonzero_count;
nums[current_not_zero_index++] = nums[i];
}
}
for (int j = nonzero_count; j < len; j++) {
nums[j] = 0;
}
}
下面的代码是Leetcode上最快的解法,使用了remove来移除vector中的0值
void moveZeroes(vector<int>& nums) {
ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n1 = nums.size();
vector <int>::iterator pos;
pos = remove (nums.begin(), nums.end() , 0); // 指向未被remove元素的后面一个元素的
for(auto i = pos; i != nums.end(); ++i)
*i = 0;
}
下面介绍remove()
,功能和我上面写的代码差不多
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) { // 只有值不为val的元素才会被添加到result中
*result = *first;
++result;
}
++first;
}
return result;
}
remove的返回值是 指向最后一个未被remove元素的后面一个元素的迭代器,在这里是指向第一个0的迭代器
An iterator to the element that follows the last element not removed.
The range between first and this iterator includes all the elements in the sequence that do not compare equal to val.
举例说明remove函数的处理过程
输入: [0,1,0,3,12]
[0,1,0,3,12] // 遍历到input[0]
[1,1,0,3,12] // 遍历到input[1]
[1,1,0,3,12] // 遍历到input[2]
[1,3,0,3,12] // 遍历到input[3]
[1,3,12,3,12] // 遍历到input[4]
remove后输出:[1,3,12,3,12]
Reference
原文地址:https://www.cnblogs.com/arcsinw/p/12680834.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Best practice on when to use the wrapper class and primitive type in Java
- Flutter基础widgets教程-BottomNavigationBar篇
- 直播系统介绍
- Azure内容审查器之羞羞图审查
- Clickhouse简介和性能对比
- Clickhouse创建分布式表以及表引擎介绍
- Azure 内容审查器之文本审查
- Redis中String数据类型原理实现
- Clickhouse分布式集群搭建
- Redis过期策略以及淘汰机制
- 几行代码就可以轻松给你的程序加上进度条
- git禁止在master分支push和commit
- 记录一次mybatis缓存和事务传播行为导致ut挂的排查过程
- appium教程_3.启动appium-server
- appium教程_4.adb常用命令