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.

Input: [0,1,0,3,12]
Output: [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.

题意:

将数组中,一旦有 0 元素, 统统拖到数组末尾。

思路:

两个指针base,  i 从index为0的位置出发。

指针 i 用来扫数组,若 i 对应的元素非 0 时,

直接将指针 i 对应元素赋值给指针 base 对应元素,同时 base++。

这样,指针 i 遍历完毕数组,指针 base 也将所有非0元素保存在数组了 index [0 ~ i] 的位置

最后将数组 index ( i ~ nums.length ) 的后半部分都赋值为 0。

代码:

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

[leetcode]283. Move Zeroes移零的更多相关文章

  1. [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 ...

  2. 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 ...

  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 移动数组中的零 (数组,模拟)

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

  5. 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 ...

  6. Java [Leetcode 283]Move Zeroes

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

  7. leetcode 283. Move Zeroes -easy

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

  8. LeetCode 283 Move Zeroes(移动全部的零元素)

    翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...

  9. 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 ...

随机推荐

  1. git 未能顺利结束 (退出码 1)

    Please make sure you have the correct access rightsand the repository exists.

  2. [UE4]ue4 c++学习推荐

    我由易到难推荐,不过在此之前还是先看看官方对于VS设置的推荐: https://docs.unrealengine.com/latest/INT/Programming/Development/Vis ...

  3. Hadoop2.0构成之HDFS2.0

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  4. Spark standalone运行模式

    Spark Standalone 部署配置 Standalone架构 手工启动一个Spark集群 https://spark.apache.org/docs/latest/spark-standalo ...

  5. UVA-839-二叉树-一个有意思的题目

    题意: 一颗二叉树可以看成一个杠杆,左右俩边有重量,有到支点长度,判断整个树是否平衡(根据杠杆原理),如果当前结点有左孩子,那么当前左边的重量就是左孩子的总和,右边同理 递归,发现scanf和cin的 ...

  6. Apache Maven 入门篇

    2017-11-09注释:IntelliJ IDEA 2017.2.5 x64 等新版本会安装maven,为了有好的体验 建议在安装目录找到IntelliJ IDEA 2017.2.5\plugins ...

  7. js 断点调试

  8. JS的prototype和__proto__、constructor

    看了JS的prototype和__proto__这篇文章,才感觉很清晰了,对于原型这块,以前经常把这些属性弄不清楚, 明白了之后保存下整理下: prototype: 是函数的一个属性(每个函数都有一个 ...

  9. delphi 编译生成ipa文件 adhoc步骤

    找IPA文件 开发模式ipa文件和发布模式ipa文件,路径不同. http://www.itnose.net/detail/6101808.html 一.开发模式Development 不需要真机,可 ...

  10. 列表(ul ol dl)

    Title 1 2 3 1 2 3 a 1 2 b 1 2 <!DOCTYPE html> <html lang="en"> <head> &l ...