LeetCode 283. Move Zeroes
2016-07-10 23:19
330 查看
Given an array
to the end of it while maintaining the relative order of the non-zero elements.
For example, given
be
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
这样竟然不超时。
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,
numsshould
be
[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.
这样竟然不超时。
class Solution { public: void moveZeroes(vector<int>& nums) { int len = nums.size(); bool haszero = true; while(haszero){ haszero = false; int i; for(i = 0; i < len - 1; i ++){ if(nums[i] == 0){ haszero = true; swap(nums[i], nums[i + 1]); } } len --; } } };
相关文章推荐
- 向量的叉乘 - 点到 直线距离 运用(lua代码)
- Excel表单的读取与处理 PHPExcel与Apache POI
- JPA-hibernate-oracle的运用
- 使用java.util.concurrent包处理多线程
- python day four
- 根据悍马病毒小结android病毒如何提取root权限
- RACSignal的Subscription深入
- Java HashMap工作原理及实现
- Android编程:UDP客户端和TCP客户端
- 【一天一道LeetCode】#371. Sum of Two Integers
- 【一天一道LeetCode】#371. Sum of Two Integers
- CSU - 1774 慷慨的奖励(模拟链表)
- 内存分析工具 MAT 的使用
- PAT乙级练习题B1044. 火星数字
- WebService服务创建
- Linux下MPI并行编程环境搭建配置
- UVa11582 巨大的斐波那契数 循环节计算+快速幂
- Java程序员不要错过的7款新工具
- HashMap vs ConcurrentHashMap — 示例及Iterator探秘
- JAVA_连接池、DataSource、JNDI