LeetCode —— 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入:[0,1,0,3,12]
输出:[1,3,12,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){
zIndexTop(nums,i,nums.length);
}
}
};
function swapArray(arr, index1, index2) {
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
}
function zIndexTop(arr,index,length){
if(index+1 != length){
var moveNum = length - 1 - index;
for (var i = 0; i<moveNum; i++) {
swapArray(arr, index, index + 1);
index++;
}
}
}
LeetCode —— 移动零的更多相关文章
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- 【LeetCode从零单排】No189 .Rotate Array
称号 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- 【LeetCode从零单排】No15 3Sum
称号 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...
- leetcode 移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 这道题比较好想出来 /** ...
- 【LeetCode从零单排】No.135Candy(双向动态规划)
1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List
题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- [LeetCode] Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [LeetCode] Lemonade Change 买柠檬找零
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and ...
随机推荐
- Linux 总线、设备、驱动模型 与 设备树
1.总线.设备.驱动模型 本着高内聚.低耦合的原则,Linux 把设备驱动模型分为了总线.设备和驱动三个实体,这三个实体在内核里的职责分别如下: 设备和驱动向总线进行注册,总线负责把设备和对应的驱动绑 ...
- VUE 引入阿里图标库
本身项目是VUE, Element-UI项目, 所以内置Element-UI图标库 地址如下, https://element.eleme.cn/#/zh-CN/component/icon 使用时: ...
- wcf_first
WCF包括3部分:client(需要连接到哪里,需要调用什么),service(宿主,及其消息的公开,地址的公开),wcf服务库(提供契约名称,及其怎么干) 步骤: 1.新建wcf库,其中提供一个契约 ...
- Suse linux enterprise 11安装时更改磁盘模式为gpt的方法
在进行鸟哥linux基础篇学习时,在"第3.2.2 选择安装模式与开机 -inst.gpt"中,鸟哥用到的CentOS 7需要用指令修改磁盘模式为gpt. 先用键盘选择Instal ...
- 最大化系统并发连接数.Windows.reg
最大化系统并发连接数.Windows.reg Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentContro ...
- Java环境变量PATH和CLASSPATH
Java开发中常用到环境变量的配置,下面简单介绍下Java中经常配置的环境变量:PATH和CLASSPATH. 1.PATH环境变量 1.1 作用简介 安装完JDK(Java Development ...
- C# Excel导入数据
表 表的创建脚本 CREATE TABLE [dbo].[TB_PROJECTS_New1]( , ) NOT NULL, ) NULL, ) NULL, , ) NULL, , ) NULL, , ...
- python学习Day6 元组、字典、集合set三类数据用法、深浅拷贝
一.深浅拷贝 1. 值拷贝 ls1 = ls2 不开辟空间,指针跟着走.(直接将ls1中存放的地址拿过来,内存中不会开辟新的空间,所以你怎么变,我也跟着变.)(ls1内部的所有类型的值发生改变,l ...
- webconfig标签收集
在web项目启动时,很多因为vs没有报错,而页面跑不出来的情况,无法调试找到错误, 可以在webconfig中添加一个标签,运行项目就可以在页面显示错误 <customErrors mode=& ...
- AJAX请求方式
<!DOCTYPE html PUBLIC "‐//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...