【leetcode】Move Zeroes[easy]
来源:互联网 发布:防狼器点击软件 编辑:程序博客网 时间:2024/04/29 09:38
好久没玩这玩意了,就先挑了个最简单的开始练练手,一次ac,但是这运行时间有点捉急。
题目很简单,给你一串Int型数组,将0元素都放到最后,上代码。
for (auto it = t.begin(); it != t.end(); it++){if (*it == 0){for (auto it2 = it+1; it2 != t.end(); it2++){if (*it2 != 0){int t;t = *it;*it = *it2;*it2 = t;break;}}}}
问题可以这样考虑,先将所有非0的数字跳出来,最后将后面置为0即可。
int pos = 0;for (auto it = 0; it != t.size(); it++){if (t[it] != 0){t[pos] = t[it];pos++;}}for (int i = pos; i != t.size(); i++){t[i] = 0;}
果然这次效率很高,属于最长的那条粉色的一员。
0 0
- 【leetcode】Move Zeroes[easy]
- <LeetCode><Easy>283 Move Zeroes
- LeetCode 283. Move Zeroes (Easy)
- 【Leetcode-Easy-283】Move Zeroes
- leetcode 283[easy----Move Zeroes
- Leetcode 283. Move Zeroes (Easy) (cpp)
- 【leetcode】【Easy】【283. Move Zeroes】【array】
- 283. Move Zeroes (Easy)
- leetcode 283 Move Zeroes(难易度:Easy)
- 283. Move Zeroes [easy] (Python)
- Easy 283题 Move Zeroes
- 283. Move Zeroes 难度:easy
- leetcode 283: Move Zeroes
- leetcode - Move Zeroes
- Leetcode: Move Zeroes
- LeetCode - Move Zeroes
- LeetCode-Move Zeroes
- [LeetCode]Move Zeroes
- 动态顺序栈的定义
- 认识smack3.0.4基本对象——From
- 【分享】PRIMAL×HEARTS1+2 (プライマルハーツ‐)【日文硬盘版】(带全CG存档&日文攻略+打开存档补丁
- 一个轻量级rest服务器
- k-means聚类算法
- 【leetcode】Move Zeroes[easy]
- Arduino控制L298N
- 博客存在的意义和最近的状态
- 动态链式队列
- Day 5(11.7):(4)运算符及函数
- 解决VS2010链接错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 黑马程序员——多线程--线程的创建方式和线程安全的简单介绍
- ZOJ 2970 水(F)
- str and repr 函数