LeetCode之283. Move Zeroes
----------------------------------------------------------------------
解法一:空间换时间
我使用的办法也是类似于“扫描-拷贝”这种的,不过稍微有些不同,是使用了一个队列来记录空闲的位置信息,然后每次需要移动的时候出队列就可以了,这样可以做到最少的拷贝次数。
扫描到一个元素的时候情况可能有以下几种:
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的更多相关文章
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- [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 ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- 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 ...
- 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 ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
- 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 ...
- 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 ...
随机推荐
- 结构体里的“位域”(bit-field)结构
首先看一个题目: #include <iostream> using namespace std; #include <string.h> typedef struct AA{ ...
- 牛顿法|阻尼牛顿法|拟牛顿法|DFP算法|BFGS算法|L-BFGS算法
一直记不住这些算法的推导,所以打算详细点写到博客中以后不记得就翻阅自己的笔记. 泰勒展开式 最初的泰勒展开式,若 在包含 的某开区间(a,b)内具有直到n+1阶的导数,则当x∈(a,b)时,有: ...
- jQuery animate动画 stop()方法详解~
一.动画格式: 格式一:jQueryObject.animate( cssProperties, options ) 格式二:$('#id').animate( styles[, duration ] ...
- 漫谈JVM
背景介绍 JVM已经是Java开发的必备技能了,JVM相当于Java的操作系统. JVM,java virtual machine, 即Java虚拟机,是运行java class文件的程序. Java ...
- 使用 Eclipse 调试 Java 程序的 10 个技巧
你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...
- linux中给PHP安装mongodb的扩展
centos5.6 32bit php 5.2.17 php安装路径 /usr/local/php phpize路径 /usr/bin php-config路径 /usr/bin php.ini路径 ...
- 【转载】使用Pandas对数据进行筛选和排序
使用Pandas对数据进行筛选和排序 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas对数据进行筛选和排序 目录: sort() 对单列数据进行排序 对多列数据进行排序 获取金额最小前10项 ...
- Java 判断整数方法
今天写代码的时候突然想到要怎么来判断整数,然后通过判断是否是整数来处理相关的操作.开始想到了几个方法,比如百度到的 x(int) instanceof Integer,但是这样的话程序会报错,还有一个 ...
- XML-->DTD&Schema Notes
The need for XML “schemas” •Unlike any other data format, XML is totally flexible, elements can be ...
- PHP常量详解:define和const的区别
常量是一个简单值的标识符(名字).如同其名称所暗示的,在脚本执行期间该值不能改变(除了所谓的魔术常量,它们其实不是常量).常量默认为大小写敏感.通常常量标识符总是大写的. 可以用 define() 函 ...