题目描述:

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. Android UI学习1:控件和基本事件的响应

    在任何一个 GUI 系统中,控制界面上的控件(通常称为控件)都是一个基本的内容.对于 Android 应用程序,控件称为 View. 在 Android 中,在处理 UI 中的各种元素的时候,两个程序 ...

  2. List<t>中如何将指定元素的值放到第一位

    public static List<GCountryExtend> GetList() { try { var result = new List<GCountryExtend&g ...

  3. 数据库 mysql 优化器原理

    MySQL查询优化器有几个目标,但是其中最主要的目标是尽可能地使用索引,并且使用最严格的索引来消除尽可能多的数据行. 你的最终目标是提交SELECT语句查找数据行,而不是排除数据行.优化器试图排除数据 ...

  4. iOS7.1Https企业证书发布方法

    openssl使用的是macos系统自带的版本,关键点是不同直接使用ios设备打开https的链接,需要将证书发到系统的mail里,安装到设备, 如果命令执行不成功,用sudo执行. 1.生成服务器的 ...

  5. UITableView自定义单元格

    随手笔记: RootViewController代码 #import "RootViewController.h" #import "AddressContact.h&q ...

  6. C语言基础(转载自大海笔记)

    # C语言基础2015年03月26日10:04:411.    语言排行榜C——java——objective-C2.    进制:进制:进位机制.用普通的话讲,应该为人为的定义一种度量来标识一样东西 ...

  7. flex toolTip样式设置

        需要3个文件.一个是样式类,一个样式文件,一个是mxml文件. ●MyToolTip.as package{ import mx.core.UITextField; import mx.ski ...

  8. winform访问url传参有返回值

    using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;usi ...

  9. bzoj 3676: [Apio2014]回文串 回文自动机

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 844  Solved: 331[Submit][Status] ...

  10. <base target="_blank"/>

    <base target=_blank> 是将基本链接的目标框架都改为新页打开