题目描述:

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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

解题思路:

将数组中的非零数字往前压缩,剩余的位置补零。

代码如下:

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

  

Java [Leetcode 283]Move Zeroes的更多相关文章

  1. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  2. LeetCode 283. Move Zeroes (移动零)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  3. [LeetCode] 283. Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  4. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  5. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  6. 11. leetcode 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  7. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  8. [leetcode]283. Move Zeroes移零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  9. Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)

    题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...

随机推荐

  1. Python垃圾回收机制详解

    一.垃圾回收机制 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 在Python中,如果一个对象的引用数为0,Python虚拟机就会回收这个对象的内存. #e ...

  2. ubuntu 设置显示器的亮度

    ubuntu电脑重新启动后,亮度都变成了最亮.似乎也没胡地方可以设置.只好通过写个脚本来做这个事了. # -*- coding: utf-8 -*- import dbus bus = dbus.Se ...

  3. Unity3d之Http通讯GET方法和POST方法

    (一)GET方法 IEnumerator SendGet(string _url) { WWW getData = new WWW(_url); yield return getData; if(ge ...

  4. random note

    今天才慢慢意识到,什么才是学习,(以思考解决问题为驱动),埋头刷分只是方法,不是目的和原动力. 既然准备读研,就要慢慢去了解研究生的生活学习方式是什么样的,涉及到哪些方面. 读研之前要选好方向,但是现 ...

  5. python多进程中的队列数据共享问题

    0x00 起 今天在写一个小东西的时候,需要控制并发量,但又不能直接调用python multiprocessing(问题会在文后提到).于是尝试用Queue来实现. 最一开始的思路是这样的: fro ...

  6. Linux:crontab命令学习

    一.crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动cro ...

  7. xcode7 app loader error itms 90168

    . $ cd ~/.itmstransporter   . $ rm update_check*   . $ mv softwaresupport softwaresupport.bak   . $  ...

  8. WDS无线桥接

    因为放假回家,长时间不在家,家里也没什么人,所以也就没有网可以用.为了两个月办宽带又不值得,太过浪费了.于是就只能蹭网用了.当然,要和邻居打个招呼或者你能搞定密码的情况下不打招呼(嘿嘿...).但是有 ...

  9. AFNetworking 简单应用

    最近最学习 AFNetworking ,根据自己所学对 AFNetWorking 一些简单应用做了一下简单封装,主要有 get,post形式获取 xml,json,get 方式获取图片,下载文件,上传 ...

  10. sql视图学习笔记--视图

    视图是为用户对数据多种显示需求而创建的,其主要用在一下几种情况: (1)限制用户只能访问特定表特定条件的内容,提高系统的安全性. (2)隐藏表结构.创建多种形式的数透视,满足不同用户需求. (3)将复 ...