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 (let i = nums.length - 1; i >= 0; i--) {
if (!nums[i]) {
nums.splice(i, 1);
nums.push(0);
}
}
};
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 ...
随机推荐
- PHP - 模拟HTTP请求, stream_context_create 和 fopen 和 fsockopen
一.fsocketopen,使用这个的过程看起来更像别的语言的socket编程 public function send($request) { /* 请求数据 */ $post_data = $re ...
- sqlserver中xml查询
DECLARE @DOC XML =' <books> <book category="C#"> <title language="e ...
- JsSIP 注册,拨打填坑
吐个槽: http://tryit.jssip.net/ 这个毛东西,默认是要使用视频的,而且没得设置不使用,至少我没看到有设置的!!!(其实就是写死了,翻了他的js代码的-_-||) 设置连接到自 ...
- Application.LoadLevel & Object.DontDestroyOnLoad
[Application.LoadLevel] 只有在File->Build Setting中设置了的按钮才能被加载. 当level加载完成后,MonoBehaviour.OnLevelWasL ...
- Excel VBA入门(二)数组和字典
数组和字典也是VBA的常用到数据类型之一.但是我翻了有四五本VBA教程相关的书,里面都没有介绍到字典,数组到是在介绍数据类型时有介绍,而并没有提到字典. 事实上,字典不是VBA内置的类型,它是Wind ...
- linux ifconfig显示 command not found
本人装的是centos7 想看下网络配置 结果显示如图: 正常情况下 ifconfig 是在超级管理员 的所属的目录 sbin/下的命令 现在来查看该目录下. 没有找到,别急 用 yum sear ...
- 微信小程序开发教程,大多数人都搞错的八个问题
小程序目前被炒得沸沸扬扬,无数媒体和企业借机获取阅读流量. 这再次证明一点,微信想让什么火,真的就能让什么火.这种能力真是全中国再也没有人有了,政府也没有. 但四处传的消息很多是失真的,废话不说,先列 ...
- VM 监控信息布局
<div ng-show="showVmChart"> <div class="row"> <div class="co ...
- 28-组合数(dfs)
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=32 组合数 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- sqlserver三种数据集合运算
2.1 并集运算(UNION) (1)UNION ALL(不删除重复行) Code: 1 SELECT empID,empName,position,degree 2 FROM Employees ...