给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。
 /**
* @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 —— 移动零的更多相关文章

  1. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

    题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...

  2. 【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 ...

  3. 【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 ...

  4. leetcode 移动零

    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 这道题比较好想出来 /** ...

  5. 【LeetCode从零单排】No.135Candy(双向动态规划)

    1.题目 There are N children standing in a line. Each child is assigned a rating value. You are giving ...

  6. 【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 ...

  7. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  8. [LeetCode] Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  9. [LeetCode] Lemonade Change 买柠檬找零

    At a lemonade stand, each lemonade costs $5.  Customers are standing in a queue to buy from you, and ...

随机推荐

  1. JAVA的第二次作业

    1.编写“人”类及其测试类.1.1 “人”类: 类名:Person 属性:姓名.性别.年龄.身份证号码 方法:在控制台输出各个信息1.2 测试类 类名:TestPerson 方法:main ...

  2. matplotlib.mlab库的重要函数

    连接地址 matplotlib.mlab¶ 与 MATLAB兼容的函数 MATLAB compatible functions¶ cohere() Coherence (normalized cros ...

  3. card布局解决复杂操作的布局问题

    一直不是很待见直接使用card布局,直到对于一些稍微复杂点的业务, 通过border布局和弹窗体的方式解决特别费劲之后,才想起了card布局, 发现card布局真是一个很好的解决办法. 那个使用起来很 ...

  4. jquery遍历----end()方法

    定义和用法 end() 方法结束当前链条中的最近的筛选操作,并将匹配元素集还原为之前的状态. 举个栗子: <body> <ul class="first"> ...

  5. eos dapp开发

    https://blog.csdn.net/u010665359/article/details/82906497

  6. hdu2586How far away ?-(LCA)

    http://acm.hdu.edu.cn/showproblem.php?pid=2586 题意:有n个点,有n-1条线连通,求两点间的最短距离,最近公共祖先的入门题.Tarjan离线算法. #in ...

  7. 7、...arg ...[1,2,3] 数组扩展

    1.将离散的参数转成数组 2.将数组拆成单个离散的值 https://blog.csdn.net/qq_30100043/article/details/53391308 箭头函数写法 函数名 -&g ...

  8. ajax请求aspx.cs后台方法

    前台jquery代码 $(function () { $("#btnfix").click(function () { $.ajax({ type: "post" ...

  9. 音视频处理概要 markdown

    最近要想办法把录制的音视频进行拼接. 比方说此次录制的视频有三段,通过高清直播编码器录制,录制下的标准为h.264 直接用ffmpeg简单拼接,音频会丢失,所以有了此次解决方案(有可能会繁琐,简单方案 ...

  10. AttributeError: 'LoginForm' object has no attribute 'is_bound' , object has no attribute 'is_bound'

    'LoginForm' object has no attribute 'is_bound' 可能原因: 啥子问题?? 都是 jquery.js 文件.....