----------------------------------------------------------------------

解法一:空间换时间

我使用的办法也是类似于“扫描-拷贝”这种的,不过稍微有些不同,是使用了一个队列来记录空闲的位置信息,然后每次需要移动的时候出队列就可以了,这样可以做到最少的拷贝次数。

扫描到一个元素的时候情况可能有以下几种:
nums[i]==0   --> 放入下标队列,没有元素移动
nums[i]!=0 && !queue.isEmpty()  --> 从位置队列头取出一个位置j,将这个值nums[i]赋给取出的位置nums[j],同时将此位置i(值已移动,可用)放入位置队列
nums[i]!=0 && queue.isEmpty()  --> 说明之前的都已经好了,不需要进行任何操作

时间复杂度是线性级别的,以空间换取时间。

AC代码如下:

public class Solution {
public void moveZeroes(int[] nums) {
List<Integer> queue=new ArrayList<>();
for(int i=0;i<nums.length;i++){
if(nums[i]!=0 && !queue.isEmpty()){
nums[queue.remove(0)]=nums[i];
queue.add(i);
}else if(nums[i]==0){
queue.add(i);
}
}
Arrays.fill(nums,nums.length-queue.size(),nums.length,0);
}
}

解法二:维护两个指针 O(n^2)

维护两个指针,不断的往前移动。

AC代码:

public class Solution {
public void moveZeroes(int[] nums) {
int i=0,j=0;
while(i<nums.length){
if(nums[i]==0){
j=Math.max(i,j);
while(j<nums.length && nums[j]==0) j++;
if(j==nums.length) return ;
nums[i]=nums[j];
nums[j++]=0;
}
i++;
}
}
}

题目来源: https://leetcode.com/problems/move-zeroes/

LeetCode之283. Move Zeroes的更多相关文章

  1. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  2. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  3. [leetcode]python 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 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

  5. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

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

  7. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

  8. 283. Move Zeroes(C++)

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

  9. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

随机推荐

  1. Android 手机怎么录屏制成gif图片

    参考:http://www.cnblogs.com/dasusu/p/4903511.html 上面的博主说的很详细了,但作为学习记录我就重新写一遍帮助自己加深记忆 一.准备条件 1.你搭建了Andr ...

  2. python命令行下安装redis客户端

    1. 安装文件: https://pypi.python.org/pypi/setuptools 直接下载然后拷贝到python目录下同下面步骤 下载 ez_setup.py>>> ...

  3. 安装zookeeper遇到的问题以及解决方案

    伪分布式安装基本思想 zookeeper的安装包保存一份,但是zoo.cfg配置多份,启动zookeeper服务器的时候指定不同的zoo.cfg即可.即启动时这样启动:zkServer.sh star ...

  4. eclipse发布web项目到生产环境的方式汇总(tomcat)

    参考: http://www.cnblogs.com/xiohao/p/3689832.html http://www.111cn.net/jsp/J2EE-EJB/90337.htm 我收集了几点: ...

  5. 将DataTable中的数据导出到Excel

    public static void Export(System.Data.DataTable dt,NPOI.HSSF.UserModel.HSSFWorkbook workbook,string ...

  6. <<< PermGen space溢出解决方法

    错误信息中的PermGen space的全称是Permanent Generation space,是指内存的永久保存区域.还没有弄明白PermGen space是属于非堆内存,还是就是非堆内存,但至 ...

  7. Logstash-5.0同步.json文件到ElasticSearch-5.0配置文件

    logstash/conf/input-file.conf内容如下: input { file { #监听文件的路径. path => ["E:/data_json/*.json&qu ...

  8. Liunx下查看服务器硬件信息

    一.如何查看服务器的CPU 今天安装了9台Linux服务器,型号完全不一样(有DELL. HP和IBM服务器),又懒得去对清单,如何在Linux下cpu的个数和核数呢?另外,nginx的cpu工作模式 ...

  9. MYSQL开启慢查询日志实施

    查看当前服务器是否开启慢查询:1.快速办法,运行sql语句show VARIABLES like "%slow%" 2.直接去my.conf中查看.my.conf中的配置(放在[m ...

  10. Bootstrap学习笔记

    Bootstrap提供了一套响应式.移动设备优先的流式栅格系统. Bootstrap把一个容器或整个网页平均分成了12列. 栅格系统必须放在.container或container-fluid中 样式 ...