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

解法一:空间换时间

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

扫描到一个元素的时候情况可能有以下几种:
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. 即使用ADO.NET,也要轻量级实体映射,比Dapper和Ormlite均快

    不管出于什么原因,有时候框架人员摒弃了NH或EF,而使用原生数据库访问对象. 为了优美的编程,用上我写的轻量级映射扩展方法吧 目的:将SqlDataReader自动转换成T类型 代码如下: /// & ...

  2. 项目分析_xxoo-master

    项目介绍:使用java1.5的原生xml操作类实现 对象<-->xml字符串的相互转化 项目分析:主要分为是三个部分 1.容器类:AbstractContainer         存储x ...

  3. 在渲染前获取 View 的宽高

    在渲染前获取 View 的宽高 这是一个比较有意义的问题,或者说有难度的问题,问题的背景为:有时候我们需要在view渲染前去获取其宽高,典型的情形是,我们想在onCreate.onStart.onRe ...

  4. 【USACO 2.3】Money Systems(dp)

    v种货币,求有多少种组成和为n. dp[i][j]表示前i种货币价格为j有多少种方案,dp[i][j]+=dp[i-1][j-c]. http://train.usaco.org/usacoprob2 ...

  5. iOS获取本机IP地址

    #import <ifaddrs.h> #import <arpa/inet.h> // Get IP Address - (NSString *)getIPAddress { ...

  6. <<< chm格式文件打不开及一些问题

    CHM 意为 Compiled HTML.以CHM为扩展名的文件图标通常为一个带问号的文档图标,表示帮助文档,是 Microsoft 自 Windows 98 以来提供的一种帮助文档格式的文件,用于替 ...

  7. 新手理解HTML、CSS、javascript之间的关系

    http://www.cnblogs.com/dreamingbaobei/p/5062901.html 工作多年,一直忙忙碌碌的应用各种技术,现在不忙了,问问自己究竟在做什么,究竟会什么竟答不上来, ...

  8. <head>中<meta name="viewport" content="width=device-width,initical-scale=1"的作用>

    <meta name="viewport" content="width=device-width,initical-scale=1"的作用> co ...

  9. Python_DB_Api

    python DB API 内容 建立连接connection 数据库交互对象cursor 数据库异常类exception 流程 创建connection 获取cursor 执行查询.执行命令.获取数 ...

  10. CSS控制表格(table)样式

    CSS控制表格样式 /* 合并边框重叠部分 */ table{border-collapse:collapse;} /* 单元格边框 */ td{border:1px solid #A7AEB1;}