给定一个数组 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. leetcode687

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  2. exited with code 1

    brcc32 command line for "Project1.vrc"   c:\program files\embarcadero\rad studio\9.0\bin\c ...

  3. C++虚函数表理解

    一,思维模式图 二,代码验证 class A { public: A(int x) { fProtected = x; } float GetFProtected() { return fProtec ...

  4. requesth获取参数

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) t ...

  5. VS Code 在新Tabs打开文件

    添加如下设置即可 "workbench.editor.enablePreview": false

  6. ubuntu linux常用指令(1)

    序号 命令 说明 1 sudo su 从普通用户切换到root用户 2 su user 从root用户切换到普通用户 3 ls 列出当前目录的文件和目录,但是不包括隐藏文件和目录 4 ls -a 列出 ...

  7. 01-nginx介绍及编译安装

    第一天的时候学过apache,nginx和apache一样都是一个WEB服务器.也就是说你的网站之所以可以在网上运行,总得有一台机器在为你提供服务,这台机器就是服务器.这台服务器通过什么软件在为你提供 ...

  8. 基于HttpRunner的接口自动化测试平台HttpRunnerManager(二)

    https://github.com/HttpRunner/HttpRunnerManager HttpRunnerManager Design Philosophy 基于HttpRunner的接口自 ...

  9. iOS中NSFileManager文件常用操作整合

    //获取Document路径 + (NSString *)getDocumentPath { NSArray *filePaths = NSSearchPathForDirectoriesInDoma ...

  10. javascript运算符优先级顺序

    1 ()2 !.-(负号).++.-- 3 *./.%4 +.- 10-55 <.<=.<.>=6 ==.!=.===.!==.7 &&8 ||9 ?:10 = ...