【数组】Next Permutation
题目:
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
思路:
步骤如下:
- 从最尾端开始往前寻找两个相邻的元素,两者满足i < ii(令第一个元素为i,第二个元素为ii)
- 如果没有找到这样的一对元素则,表明当前的排列是最大的,没有下一个大的排列
- 如果找到,再从末尾开始找出第一个大于i的元素,记为j 本文地址
- 交换元素i, j,再将ii后面的所有元素颠倒排列(包括ii)
- 如果某个排列没有比他大的下一个排列(即该排列是递减有序的),调用这个函数还是会把原排列翻转,即得到最小的排列
var nextPermutation = function(nums) {
var len=nums.length;
if(len<=1){
return;
}
for(var i=len-2,j=len-1;i>=0;i--,j--){
if(nums[i]<nums[j]){
var q=len-1;
while(nums[q]<=nums[i]){
q--;
}
var temp=nums[i];
nums[i]=nums[q];
nums[q]=temp;
var a=nums.splice(q).reverse()
nums=nums.concat(a);
break;
}
}
if(i==-1){
nums.reverse();
}
};
【数组】Next Permutation的更多相关文章
- 树状数组---Squared Permutation
BNUOJ 51636 最近,无聊的过河船同学在玩一种奇怪的名为“小Q的恶作剧”的纸牌游戏. 现在过河船同学手有张牌,分别写着,打乱顺序之后排成一行,位置从左往右按照标号. 接下来小Q同学会给出个 ...
- python之numpy库[2]
python-numpy csv文件的写入和存取 写入csv文件 CSV (Comma‐Separated Value, 逗号分隔值),是一种常见的文件格式,用来存储批量数据. 写入csv文件 np. ...
- 数据分析与展示——NumPy数据存取与函数
NumPy库入门 NumPy数据存取和函数 数据的CSV文件存取 CSV文件 CSV(Comma-Separated Value,逗号分隔值)是一种常见的文件格式,用来存储批量数据. np.savet ...
- python 数据分析工具之 numpy pandas matplotlib
作为一个网络技术人员,机器学习是一种很有必要学习的技术,在这个数据爆炸的时代更是如此. python做数据分析,最常用以下几个库 numpy pandas matplotlib 一.Numpy库 为了 ...
- python数据分析及展示(一)
一.IDE选择 Anaconda软件:开源免费,https://www.anaconda.com下载,根据系统进行安装.由于下载速度慢,可以去清华大学开源软件镜像站下载. Spyder软件设置:Too ...
- python-数据分析与展示(Numpy、matplotlib、pandas)---1
笔记内容整理自mooc上北京理工大学嵩天老师python系列课程数据分析与展示,本人小白一枚,如有不对,多加指正 1.ndarray对象的属性 .ndim..shape..size(元素个数,不是占用 ...
- python中的各种模块(np,os,shutill)
PS:本博文摘抄自中国慕课大学上的课程<Python数据分析与展示>,推荐刚入门的同学去学习,这是非常好的入门视频. #np模块 .ndim :维度 .shape :各维度的尺度 (2,5 ...
- Numpy随机数
Numpy随机数 np.random随机数子库 1: 基本函数 .rand(d0,d1,..dn):创建d0-dn维度的随机数数组,浮点数,范围从0-1,均匀分布 .randn(d0,d1,..dn) ...
- numpy 模块常用方法
Numpy是科学计算库,是一个强大的N维数组对象ndarray,是广播功能函数.其整合C/C++.fortran代码的工具 ,更是Scipy.Pandas等的基础 .ndim :维度 .shape : ...
- np.random的随机数函数
np.random的随机数函数(1) 函数 说明 rand(d0,d1,..,dn) 根据d0‐dn创建随机数数组,浮点数, [0,1),均匀分布 randn(d0,d1,..,dn) 根据d0‐dn ...
随机推荐
- $clog2(转)
(转http://www.xilinx.com/support/answers/44586.html) 13.2 Verilog $clog2 function implemented imprope ...
- 2015 - 4- 21 iOS开发越狱环境的搭建1
2015 - 4- 20 1. 越狱环境的搭建 http://www.iduuke.com/2030.html http://www.cnblogs.com/xiongwj0910/archi ...
- [转] ubuntu安装Sublime Text 3并使用SublimeClang插件
原文地址:https://blog.csdn.net/cywosp/article/details/32721011 3. 安装强大的SublimeClang插件 SublimeClang是Subli ...
- shell 脚本 批量修改文件名
修改文件名前 #!/bin/bask # for a in $( ls /etc/yum.repos.d/CentOS* );do if [ $a != '/etc/yum.repos.d/CentO ...
- WebApi使用JWT认证(一)
这是第一部:先实现NetFramework上的WebApi使用JWT认证 1.VS新建一个WebApi项目 2.项目右键----管理Nuget程序包----找到JWT,然后安装 3.Model文件夹下 ...
- [LeetCode] Binary Tree Maximum Path Sum(最大路径和)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- MVC2 阻止公共方法被调用
阻止公共方法被调用 using System.Web.Mvc; namespace MvcApplication1.Controllers { public class WorkController ...
- Eclipse 4.2 failed to start after TEE is installed
--------------- VM Arguments--------------- jvm_args: -Dosgi.requiredJavaVersion=1.6 -Dhelp.lucene ...
- linux系统编程之文件与IO(五):stat()系统调用获取文件信息
一.stat()获取文件元数据 stat系统调用原型: #include <sys/stat.h> int stat(const char *path, struct stat *buf) ...
- webapi 用腾讯云手机短信验证码
#region 验证码相关 public class SmsSDK { #region 短信发送的必要参数 /// <summary> /// appId /// </summary ...