283 Move Zeroes
/**
* 题意:将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的更多相关文章
- 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 ...
- 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 ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
- 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@python
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)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- [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 ...
- 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 ...
随机推荐
- 常用的主机监控Shell脚本
最近时不时有朋友问我关于服务器监控方面的问题,问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能 ...
- Java api 入门教程 之 JAVA的Date类与Calendar类
在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理. 一.这里简单介绍一下Date类的使 ...
- 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法(转)
(转自:http://blog.csdn.net/bombzhang/article/details/12676789) 有一次升级开发工具后发现xml脚本出现错误“Unexpected namesp ...
- sql查询最大的见多了,查询第二的呢???
问题: 数据库中人表有三个属性,用户(编号,姓名,身高),查询出该身高排名第二的高度. 建表语句 create table users ( id ,) primary key, name ), he ...
- HTTP 请求报文 响应报文
引言 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为了提供一种发 ...
- 10、WGET
这个我看过比较好的 http://www.cnblogs.com/peida/archive/2013/03/18/2965369.html WGET 支持HTTP和FTP协议,断点续传功能,自动递 ...
- Floyd算法解决多源最短路径问题
Floyd-Warshall算法是解决任意两点间的最短路径的一种算法,可以正确处理有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭包. Floyd-Warshall算法 ...
- MMORPG大型游戏设计与开发(客户端架构 part5 of vegine)
客户端异常捕获,是一件必然的事情,特别是在开发的时候就更需要这些有利于找出问题原因的捷径.区别于服务器的是,客户端基本上是以界面为主,你很难在正常运行程序的情况下进行一些输出的监视,如一些日志的记录. ...
- python黑客编程之端口爆破
#coding:utf-8 from optparse import OptionParser import time,re,sys,threading,Queue import ftplib,soc ...
- url编码base编码解码十六进制
0x25346425353425343525333525343325366125343525373725346425353125366625373825346425343425363725346225 ...