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

解法一:空间换时间

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

扫描到一个元素的时候情况可能有以下几种:
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. JS数组转字符串和字符串转数组

    主要用到以下两个函数 join();//将数组转换成字符串split();//将字符串转换成数组 <script type="text/javascript"> var ...

  2. Github代理设置

    启用代理 git config --global http.proxy http://proxyuser:proxypwd@proxy.server.com:8080 git config --glo ...

  3. Gulp和Webpack工具的区别

    引用知乎的回答:https://www.zhihu.com/question/37020798 怎么解释呢?因为 Gulp 和 browserify / webpack 不是一回事 Gulp应该和Gr ...

  4. Google开源库-Volley的使用

    一.什么是Volley? Volley is an HTTP library that makes networking for Android apps easier and most import ...

  5. 选择QT作为自己的图形库

    图形库太多,公司里面一直使用自己的图形库,换一家公司,就换个图形库,现在公司没有对我开放图形库代码. 想来想去还是自己要有一套图形库,拿来主义最方便,选来选去感觉还是QT比较方便.同时能学习一下C++ ...

  6. 谷歌地图地理解析和反解析geocode.geocoder详解

    地址解析就是将地址(如:贵州省贵阳市)转换为地理坐标(如经度:106.71,纬度:26.57)的过程. 地理反解析和上面的过程相反是将地理坐标(如纬度:26.57,经度:106.71)转换为地址(中国 ...

  7. 网络中两台主机的通信过程(TCP)

    两台主机通信有两种情况:1.在同一网段中 2.不在同一网段中 (1.)在同一网段的通信过程 主机在应用层上的操作: TCP/IP协议上tcp的端口对应的各种应用程序,客户机要访问某个应用程序就会要求打 ...

  8. arguments

    arguments 转数组 通常使用下面的方法来将 arguments 转换成数组: Array.prototype.slice.call(arguments); 还有一个更简短的写法: [].sli ...

  9. JAVA中获取当前系统时间及格式转换

    JAVA中获取当前系统时间   一. 获取当前系统时间和日期并格式化输出: import java.util.Date;import java.text.SimpleDateFormat; publi ...

  10. JUnit4 中@AfterClass @BeforeClass @after @before的区别对比

    JUnit4使用Java5中的注解(annotation),以下是JUnit4常用的几个annotation: @Before:初始化方法   对于每一个测试方法都要执行一次(注意与BeforeCla ...