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 ...
随机推荐
- <a>标签,鼠标经过或者停留触发延时响应事件
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAcUAAAEoCAIAAACmeX2PAAAgAElEQVR4nOzdd3xUdb74f3+Pu3v33t ...
- MySQL事务学习-->隔离级别
MySQL事务学习-->隔离级别 6 事务的隔离级别 设置的目的 在数据库操作中,为了有效保证并发读取数据的正确性,提出的事务隔离级别. 数据库是要被广大客户所共享访问的,那么在数据库操作过程中 ...
- Python特殊语法--filter、map、reduce、lambda
一.filter(function, sequence) 对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple( ...
- Linux shell if [ -n ] 正确使用方法
if [ str1 = str2 ] 当两个串有相同内容.长度时为真 if [ str1 != str2 ] 当串str1和str2不等时为真 if [ -n str1 ] 当串的长度大于0时为真( ...
- php 批量更新某字段内容的部分内容 replace(要替换的字段,'被替换的字符串,'替换成的字符串')
要求: 一个字段值: ------预约---- 要将其中 "预约",改成"预定". 但是我开始写的时候,写成了 update 表名 set smscontent ...
- 微信公众平台C# SDK:Senparc.Weixin.MP.dll
https://github.com/Senparc/WeiXinMPSDK [转] http://www.cnblogs.com/szw/archive/2013/01/13/senparc-wei ...
- 【温故而知新-Javascript】使用canvas元素(第一部分)
1. 开始使用 canvas 元素 canvas 元素非常简单,这是指它所有的功能都体现在一个JavaScript对象上,因此该元素本身只有两个属性:width 和 height. canvas 元素 ...
- 怎样运用好ZBrush中的布尔运算
我们知道DynaMesh常用于基础模型的起稿到中模的制作,它是ZBrush ® 4R2新增的功能,其强大的功能除了对模型进行重新布线,还可以进行布尔运算.配合Insert笔刷进行布尔运算,可以做出Z ...
- AC日记——整理药名 openjudge 1.7 15
15:整理药名 总时间限制: 1000ms 内存限制: 65536kB 描述 医生在书写药品名的时候经常不注意大小写,格式比较混乱.现要求你写一个程序将医生书写混乱的药品名整理成统一规范的格式,即 ...
- jsp前三章测试
(选择一项) A: B: C: D: 正确答案是 B ,B/S架构并不是C/S架构的替代品,有些程序例如大型的网络游戏一般使用的是C/S架构. (选择多项) A: B: C: D: 正确答案是 A,C ...