LeetCode——Move Zeroes
2015-09-26 21:37
441 查看
Description:
Given an array
For example, given
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
时空复杂度都在O(n)内
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,
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.
时空复杂度都在O(n)内
public class Solution { public void moveZeroes(int[] nums) { //nums = [0, 1, 0, 3, 12]->[1, 3, 12, 0, 0] for(int i=0,j=0; i<nums.length; i++) { if(nums[i]!=0) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; j ++; } } } }
相关文章推荐
- [Qt] IP地址输入框实现
- 管道和FIFO
- Shell脚本中利用awk处理大批量数据
- 附5、MDT 2013 Update 1批量部署-更新版无法修改共享文件夹参数的解决办法
- 对最近几次应聘的总结
- Good Luck in CET-4 Everybody!(博弈)
- Exchange工具12—Remote Connectivity Analyzer
- POJ1741 点分治
- github创建多个sshkey
- js计时器
- java从命令行接收多个数字,求和之后输出结果
- Eclipse 使用OpenGL
- python - bing_api_search
- Action调用过程分析
- 【沟通的艺术】自我反思的技术演示
- 关于view位移后无法获取点击事件的情况
- 软件测试基础知识整理
- 第108讲:Akka中的Actor消息的处理、消息回复、消息转发学习笔记
- 动画基础
- [C#]INI文件控制类