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 ...
随机推荐
- shell脚本修改文本中匹配行之前的行的方法
原创文件,欢迎阅读,禁止转载. 例子中是把 finish 前一行的 "yes" 改成 "YES"有一个方法就是利用sed+awk一起来完成. zjk@zjk:~ ...
- Mybatis pageHelper.startPage(...)是物理分页
使用PageHelper.startPage(...)进行物理分页 业务需求只显示其中的100条数据 之前是在业务逻辑里对参数limit进行了处理 后来试试sql的limit查询100条数据 但是不确 ...
- Mac+sublime text+skim
首先:安装Mac Tex .Sublime.skim MacTex: https://tug.org/mactex/mactex-download.html Sublime3 : http:// ...
- Redis 4.0.2.3 for Windows (alpha) 下载地址
下载地址如下: https://github.com/tporadowski/redis/releases 如果直接使用redis-server.exe启动报错的话,就使用redis-server.e ...
- 使用TensorFlow训练模型的基本流程【转】
原文地址(https://github.com/wmpscc/TensorflowBaseDemo ) 本篇文章将介绍使用tensorflow的训练模型的基本流程,包括制作读取TFRecord,训练和 ...
- struts2.5入门
引用链接:https://www.cnblogs.com/qulianqing/p/6627746.html
- React Native: unable to load scripts from assets 'index.android.bundle' on real device
问题:重新建了一个项目后,运行react-native run-android报: unable to load scripts from assets 'index.android.bundle' ...
- 在create-react-app里使用ant design
使用create-react-app创建的项目,要使用ant design. 1.首先进入项目根目录,yarn add antd. 2.在App.css引入 样式文件,@import '~antd/d ...
- mac 下直接给docker容器加映射 mysql 为例
如果你是下面这种情况,本文可能回给你一些帮助 os是Mac,docker中已有mysql容器,并且已经有数据,但是没有设置映射,想要从主机连接docker 中的mysql,以便更好的查看,增加,删除数 ...
- Java简单操作dubbo(一)
dubbo-service公共Service package com.itman.service; public interface UserService { // 提供服务 使用userId查找用 ...