/**
* 题意:将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. oracle报错:ORA-00054: 资源正忙,要求指定 NOWAIT

    ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源: --首先得到被锁对象的session_idselect session_id from v$locked_object; -- ...

  2. java统计汉字

    public class TotalUtil { public static int getSum(String text) {        String reg = "^[\u4e00- ...

  3. JUnit4生命周期

  4. Python引用模块和查找模块路径

    模块间相互独立相互引用是任何一种编程语言的基础能力.对于"模块"这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义.对于编译 ...

  5. emacs 新手笔记(二) —— 分割窗格 (split window)

    初极狭,才通人.复行数十步,豁然开朗.—— 陶渊明·桃花源记 ilocker:关注 Android 安全(新入行,0基础) QQ: 2597294287 使用 split-window-xxx 函数可 ...

  6. stm32 USART rs485 rs232

    转载自:http://www.cnblogs.com/chineseboy/archive/2013/03/06/2947173.html 前题: 前段时间,在公司调试了一个项目,很简单,但对于初学的 ...

  7. windows 10 & Office 2016 安装

    Office 2016 VOL版    http://blog.sina.com.cn/s/blog_470614a90102vtmc.html 专业版合集: magnet:?xt=urn:btih: ...

  8. 单机搭建Android开发环境(三)

    单机搭建Android开发环境,第一篇重点介绍了如何优化Windows 7系统,以提高开发主机的性能并延长SSD的使用寿命.第二篇重点介绍了基于VMWare安装64位版的Ubuntu 12.04,并安 ...

  9. Codeforces 549B. Looksery Party[构造]

    B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  10. Java中的移位运算符

    java中有三种移位运算符 <<      :     左移运算符,num << 1,相当于num乘以2 >>      :     右移运算符,num >& ...