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. [UE4]C++创建对象的三种方式

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  2. Spark分析之BlockManager

    BlockManager中存储block的流程: doPut()方法   入参:blockId, data, level, tellMaster 1)为block创建BlockInfo并加锁使其不能被 ...

  3. java实现远程控制

    屏幕监控: Robot robot = new Robot();Dimension d = Toolkit.getDefaultToolkit().getScreenSize();image = ro ...

  4. 使用seaborn制图(小提琴图)

    import numpy as np import pandas as pd import matplotlib.pyplot as plt import seaborn as sns # 设置风格, ...

  5. favicon.ico

    favicon.ico 作为网页的图标,被当前的所有浏览器都支持. 可直接放在主目录下,自动加载,也可设置在header中. <link rel="shortcut icon" ...

  6. EL&jsp

    JSP 2.0(java server pages): EL 表达式 JSP九大内置对象及作用范围 JSP Directive JSP Action EL表达式: EL 算法(Arithmetic)表 ...

  7. vue基础——vue实例

    创建一个vue实例 每个vue应用都是通过Vue函数创建一个新的Vue实例开始的 var vm = new Vue({ //选项 }) 一个Vue应用由一个通过new Vue创建的根Vue实例,以及可 ...

  8. 下载gradle缓慢的解决方法

    用AndroidStudio或者Qt编译apk,下载gradle缓慢时,可以用迅雷等下载工具在https://services.gradle.org/distributions/下载对应的版本. 中断 ...

  9. 编写jQuery插件(二)——jQuery插件类型和机制

    jQuery插件类型 jQuery插件主要有3种类型: 1.封装对象方法的插件 这种插件类型是最常见的一种插件,它将对象方法封装起来,对通过选择器获取的jQuery对象进行操作. 2.封装全局函数的插 ...

  10. SVM支持向量机推导,工具介绍及python实现

    支持向量机整理 参考: Alexandre KOWALCZYK大神的SVM Tutorial http://blog.csdn.net/alvine008/article/details/909711 ...