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. PHP下载文件的几种方案

    PHP下载远程文件的3种方法以及性能考虑 2014-02-21      0个评论       收藏    我要投稿 今天在做导出Excel的时候,总是要测试导出的Excel文件,频繁的下载和打开,很 ...

  2. Spring mvc的web.xml配置详解

    1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilter(filter-name) 2.在web.xml配置监听器ContextLoaderListener(l ...

  3. NVMe on RHEL7

    原文地址https://www.dell.com/support/article/cn/zh/cnbsd1/sln312382/nvme-on-rhel7?lang=en Posted on beha ...

  4. Django之模板Template

    模板介绍 作为Web框架,Django提供了模板,可以很便利的动态生成HTML 模版系统致力于表达外观,而不是程序逻辑 模板的设计实现了业务逻辑(view)与显示内容(template)的分离,一个视 ...

  5. 57. 激活office时出下以下问题的解决方案

      我们拿出一段来分析一下(我所知道的不多)SKU ID:1b686580-9fb1-4b88-bfba-eae7c0da31adLICENSE NAME: Office 15, OfficeProP ...

  6. spring boot 自定义异常

    1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...

  7. HTML5 Canvas ( 图片填充样式 ) fillStyle, createPattern

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. mysql:rand()产生随机整数,CONCAT拼接时间字符串

    用存储过程插入测试数据,如果不想update_time都一样,可以进行随机字符串拼接:2月随机1天,小时随机 CONCAT('2017-02-',FLOOR(1 + (RAND() * 28)),' ...

  9. Pycharm安装autopep8工具

    参考文当:https://www.cnblogs.com/heenhui2016/p/6802122.html

  10. java web 跨域

    服务器端解决跨域问题的三种方法   跨域是指html文件所在的服务器与ajax请求的服务器是不同的ip+port,例如: - ‘192.168.1.1:8080’ 与 ‘192.168.1.2:808 ...