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 ...
随机推荐
- for 和while循环 if else
for和 while循环 当你知道循环次数的话就用for循环, 不知道的时候用while循环,while必须要有结束条件 a +=1 等于 a =a+1 for i in xxx: i:是一个变量 ...
- 关于VS2010 在设计窗口时控件消失问题
我特喵的,见鬼了. 几个相同的Tabpage中添加相同toolStrip控件,每次都是第二个Tabpage中的消失,但是查看设计器下面又显示控件存在,点击也会出现,运行后就没有了,真的是奇怪. 最后经 ...
- php通过imap获取腾讯企业邮箱信息后的解码处理
最近需要在项目开发的oa中集成一个收发腾讯企业邮箱邮件的功能,今天做到了获取收件箱内容部分,imap如何获取就不写了,百度一堆,主要是关于内容的解码 主要以邮件主题解码为主,腾讯企业邮返回的数据主要有 ...
- https://github.com/gaoyangxiaozhu/DockerVI
[更新]分享一个开源项目DockerVI,一个基于NodeJS实现的Docker Swarm可视化解决方案
- Game Engine Architecture 8
[Game Engine Architecture 8] 1.Differences across Operating Systems • UNIX uses a forward slash (/) ...
- mysql访问视图提示:找不到视图
原因: 1.不存在 2.视图区分大小写(有的不区分) 3.权限问题
- python之列表及其方法---整理集
列表类,类名是list 通过list类创建对象,使用中括号 列表特性: 使用中括号括起来 中间用逗号分隔每个元素 元素可以是数字.字符串.布尔值.列表 列表中可以嵌套列表 列表中每个元素的下标从0开始 ...
- uni-app
1 路由的跳转 uni.navigateTo({ url:'/pages/home/search' }); //非tabBar页面跳转 uni.switchTab({ url:"/pages ...
- To the Max 二维dp(一维的变形)
Description Given a two-dimensional array of positive and negative integers, a sub-rectangle is any ...
- 《Java从入门到精通》学习总结2
1. 在JAVA语言中对静态方法有两点规定: 在静态方法中不可以使用this关键字 在静态方法中不可以直接调用非静态方法 2. 不能将方法体内的局部变量声明为static的 3. 引用只是存放一个对象 ...