转载请注明出处:z_zhaojun的博客

原文地址:http://blog.csdn.net/u012975705/article/details/50493772

题目地址:https://leetcode.com/problems/move-zeroes/

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:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.

解法(java):

public class Solution {
public void moveZeroes(int[] nums) {
if (nums != null) {
int length = nums.length;
for (int i = 0, j = 0; i < length; i++) {
if (nums[i] != 0) {
if (i != j) {
nums[j] = nums[i];
nums[i] = 0;
}
j++;
}
}
}
}
}

最新文章

  1. 设计模式--单例模式Singleton(创建型)
  2. sqlserver巧用row_number和partition by分组取top数据
  3. 什么是json
  4. 如何用linux远程登录windows计算机
  5. POJ 3074 Sudoku (DLX)
  6. Git命令详解(一)-个人使用
  7. 【动态规划】【KMP】HDU 5763 Another Meaning
  8. java删除数组中的第n个数
  9. 高性能C++网络库libtnet实现:IOLoop
  10. Python面向对象3:面向对象的三大特性
  11. Ubuntu Linux 解决 bash ./ 没有那个文件或目录 的方法
  12. Java之工具类:判断对象是否为空或null
  13. eclipse 关闭控制台 自动弹出
  14. nodejs中aes-128-cbc加密和解密
  15. Linux Device Tree
  16. Oracle 数存储——物理结构
  17. Win10应用《纸书科学计算器》更新啦!
  18. csharp: Converting chinese character to Unicode
  19. RPG游戏地牢设计的29个要点
  20. AngularJS(三):重复HTML元素、数据绑定

热门文章

  1. ios7与ios6UI风格区别
  2. 剑指Offer整理笔记
  3. 智能指针unqiue_ptr
  4. 树莓派 Centos7 安装EPEL 7
  5. (1) LVS基本概念和三种模式
  6. 官网Android离线文档下载
  7. 04--activiti demo
  8. python的cache修饰器
  9. Android GradientDrawable的XML实现
  10. 【bzoj1552/3506】[Cerc2007]robotic sort splay翻转,区间最值