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 ...
随机推荐
- leetcode966
class Solution(object): def spellchecker(self, wordlist: 'List[str]', queries: 'List[str]') -> 'L ...
- 回溯法 Generate Parentheses,第二次总结
class Solution { public: vector<string> ans; void helper(string& cur, int left, int right, ...
- 关于css 的兼容设置 ----笔记
在开发网页的时候,由于浏览器的差异,会造成css 代码需要写不同的样式,才能适配大部分浏览器,这很烦,但是又不得不做,我把其中用得比较多的那些,做了一个归纳,放在这里,开发的时候,直接打开这里,复制粘 ...
- Jmeter生成html报告
进入到jmeter安全目录bin下,执行以下命令 基本命令格式: jmeter -n -t test.jmx(性能测试脚本) -l testResult.csv(测试监听结果文件) -e -o tes ...
- Python序列化操作与反序列操作
一.概念 序列化:转向一个字符串数据类型序列:字符串 二.需要做序列化操作的情况1.数据存储2.网络上数据传输 从数据类型到字符串的过程叫序列化从字符串到数据类型的过程叫反序列化 三.现有序列化模块1 ...
- 供Linux学习使用的在线模拟系统
前言 我只是一名搬运工. 最近想要找一个linux服务器用于调试shell脚本,但是公司服务器又只能内网访问,外网无法使用.对安装VMWARE+Linux镜像觉得繁琐.查找了一下资料.找到了几个在线模 ...
- .net core 2.2 修改IdentityUser主键标识类型
.net core2.2,生成WebApi或者MVC项目后,Identity 1.增加ApplicationUser.cs文件,内容如下 public class ApplicationUser : ...
- Mybatis的学习1
ORM 关系数据库需要按对象来处理,出现ORM设置,列对应类的属性,行对应对应类的实例,也就是每一行对应一个新的实例,对应类是需要实现序列化(implements Serializable - im ...
- Java & C# BCD编码与十进制转换
using System;using System.Collections.Generic;using System.Text; namespace Base{ public class BCDHel ...
- vim常用配置 vimrc文件
自从接触vim,自己瞎鼓捣.vimrc也有一段时间了.收集记录一下好用的配置. 一.奇技淫巧 1.折叠代码 折叠代码常常用在代码块较长的情况下,比如一个文件里定义了很多个函数,或者注释.括号影响的阅读 ...