/**
* 题意:将0挪到末尾,并且不改数组中原有元素的顺序
* 解析:找到0元素,然后寻找其后面非0的元素,进行交换位置
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
for(var i = 0;i< nums.length;i++){
if(nums[i]==0){
var j;
for(j = i+1;j < nums.length;j++){
if(nums[j] != 0){
nums[i] = nums[j];
nums[j] = 0 ;
break;
}
}
//如果j找到最后都没有非0,表示末尾都是0了
if(j == nums.length) break;
}
}
};

  方法二

/**
* 题意:将0挪到末尾,并且不改数组中原有元素的顺序
* 解析:将所有非零元素依次插入数组,最后空余的就为0
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function(nums) {
var k =0;
for(var i = 0;i< nums.length;i++){
if(nums[i]!=0){
nums[k] = nums[i];
k++;
}
}
for(;k<nums.length;k++) nums[k] = 0;
};

  

283 Move Zeroes的更多相关文章

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

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

  3. 【leetcode】283. Move Zeroes

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

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

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

  6. 283. Move Zeroes - LeetCode

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

  7. 283. Move Zeroes@python

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

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

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

  9. [Algorithm] 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

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

随机推荐

  1. linux中send函数MSG_NOSIGNAL异常消息

    最近2周在做ineedle的国舜项目扩展,需要使用socket的tcp连接向对方发送消息,当然需求很简单,只是按照对方要求发送指定格式的消息,程序结构也非常的简单,一对多的client/server模 ...

  2. Linux 解压缩命令

    gzip 文件 压缩文件 gzip -d 文件 解压文件 zcat  查看以结尾为zip 文件 bzip2 压缩文件 bzip2 -d 文件 解压文件 bzcat 查看以结尾为zip2 文件 zip ...

  3. 异或的精彩应用 FIX_BTMAP_END

    源文件是arch/x86/include/asm/fixmap.henum fixed_addresses {#ifdef CONFIG_X86_32        FIX_HOLE,...    _ ...

  4. java怎么建立JAVA工程项目?

    File->New->Java Project;src->New->Class; 出现packet,运行出错的问题 然后如果不要包packet 的话,不要在此处填写包的名称就行 ...

  5. [uwsgi] no request plugin is loaded, you will not be able to manage requests.

    Problem: *** Operational MODE: preforking+threaded no app loaded. going in full dynamic mode uWSGI i ...

  6. C#基础---事件的使用

    一:什么是事件     事件是可以被控件识别的操作,如按下确定按钮,选择某个单选按钮或者复选框.每一种控件有自己可以识别的事件,如窗体的加载.单击.双击等事件,编辑框(文本框)的文本改变事件,等等.事 ...

  7. MMORPG大型游戏设计与开发(客户端架构 part16 of vegine)

    由于近来比较忙碌和有些困倦的原因,所以关于这部分的文章没有及时更新,一句话:让朋友们久等了!今天所讲的是客户端vengine(微引擎)中最后一个部分,就像上节所说,这一部分的内容比较多.可能有些朋友看 ...

  8. JAVA中关于并发的一些理解

    一,JAVA线程是如何实现的? 同步,涉及到多线程操作,那在JAVA中线程是如何实现的呢? 操作系统中讲到,线程的实现(线程模型)主要有三种方式: ①使用内核线程实现 ②使用用户线程实现 ③使用用户线 ...

  9. hdu-5493 Queue(二分+树状数组)

    题目链接: Queue Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  10. uwsgi+flask环境中安装matplotlib

    uwsgi+flask的python有自身的virtual environment,可以通过如下命令进入 . venv/bin/activate 虽然通过sudo apt-get install py ...