给定一个数组 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 (let i = nums.length - 1; i >= 0; i--) {
if (!nums[i]) {
nums.splice(i, 1);
nums.push(0);
}
}
};

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. Android Studio 2.3.3 添加ksoap2的引用(拒绝网上其他的忽悠),也适用于添加其他Jar的引用

  2. leetcode706

    class MyHashMap { public: vector<int> hashMap; /** Initialize your data structure here. */ MyH ...

  3. 语义分析:C语言表达式的语法树生成——Python实现

      令狐冲慢慢走近,那汉子全身发抖,双膝一屈,跪倒在雪地之中.令狐冲怒道:“你辱我师妹,须饶你不得.”长剑指在他咽喉之上,心念一动,走近一步,低声问道:“写在雪人上的,是些什么字?”   那汉子颤声道 ...

  4. 微信小程序跳转navigateTo与redirectTo

    转自:https://www.cnblogs.com/perfect-yuewei/p/8301761.html 2018-01-16 - 微信中跳转页面方法目前接触到两种 navigateTo与re ...

  5. ajax传递给后台数组参数方式

    出自:http://blog.csdn.net/lingxyd_0/article/details/10428785 在项目上用到了批量删除与批量更改状态,前台使用了EasyUI的DataGrid,用 ...

  6. rails scaffold生成的crud显示自定义的列名

    1. 访问 rails i18n 插件的官方网站 ,查看信息http://guides.rubyonrails.org/i18n.html2. 在Gemfile 中加入  测试rails4.2.1不用 ...

  7. java内存区域的分布

    读了<深入理解Java虚拟机>之后,当时理解了,过段时间又忘记了,在此做下记录,方便自我回顾,也希望能帮到想要学习虚拟机的同学. Java虚拟机在执行java程序时会把它所管理的内存分为5 ...

  8. eureka快速剔除失效服务

    eureka服务端配置 #eureka server刷新readCacheMap的时间,注意,client读取的是readCacheMap,这个时间决定了多久会把readWriteCacheMap的缓 ...

  9. kubernetes role

    https://kubernetes.io/docs/admin/authorization/rbac/

  10. python pipenv 包管理

    原文链接:https://robots.thoughtbot.com/how-to-manage-your-python-projects-with-pipenv 翻译者:Jiong 在thought ...