<LeetCode OJ> 31. Next Permutation
31. Next Permutation
Submissions: 212155 Difficulty: Medium
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,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
STL来做,秒杀
class Solution {
public:
void nextPermutation(vector<int>& nums) {
next_permutation(nums.begin(), nums.end());
}
};
此题感觉没多大意思。不好高速想出规律:
可是比較明显的规律是:
1。升序为最小组合,降序为最大组合
2,某一个数的下一个数,就是比他大的最小组合数。比方123,下一个比他大的最小组合就是132,必须从低位处理
3,stl的处理就是从低位(数组末尾)開始找起,
a)找到首个相邻的升序序列。将前一个数据与比其首次大的数交换(然后逆置后面的数)比方:123543为124533(首个升序3,5。可是4是首次比3大,则交换),此处的操作意义就是使数通过首个升序变大。
b)显然此数不是大于原数的最小数,而且后面的数逆置后才是最小的,即124533为124335。so,done!
。
//思路首先(不调用库函数):
//举例:abc,acb,bac,bca,cab,cba
class Solution {
public:
void nextPermutation(vector<int>& nums) {
if(nums.empty() || nums.size()==1)
return;
vector<int>::iterator ite1=nums.end();
ite1--;
while(true)
{
vector<int>::iterator ite2=ite1;
ite1--;//ite1始终在ite2前面一个位置(左边算作最前面)
if(*ite1 < *ite2)//升序已经出现。接着又一次从后面找首个升序位置
{
vector<int>::iterator itej=nums.end();
while(!(*ite1 < *--itej));
iter_swap(ite1,itej);//找到首个升序序列将其交换
reverse(ite2,nums.end());
return;
} if(ite1==nums.begin())
{
reverse(nums.begin(),nums.end());
return;
}
}
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50450861
原作者博客:http://blog.csdn.net/ebowtang
參考资源
【1】侯捷。《STL源代码剥析》
<LeetCode OJ> 31. Next Permutation的更多相关文章
- leetcode个人题解——#31 Next Permutation
写这题时脑子比较混乱,重写了一遍wiki大佬的解法. 算法: According to Wikipedia, a man named Narayana Pandita presented the fo ...
- [Leetcode][Python]31: Next Permutation
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 31: Next Permutationhttps://oj.leetcode ...
- [array] leetcode - 31. Next Permutation - Medium
leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- LeetCode - 31. Next Permutation
31. Next Permutation Problem's Link ---------------------------------------------------------------- ...
- leetcode 31. Next Permutation (下一个排列,模拟,二分查找)
题目链接 31. Next Permutation 题意 给定一段排列,输出其升序相邻的下一段排列.比如[1,3,2]的下一段排列为[2,1,3]. 注意排列呈环形,即[3,2,1]的下一段排列为[1 ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
随机推荐
- oracle 创建命令
环境变量设置(在Sqlplus中执行) create or replace directory filepath as 'D:\ORACLEBACKUP'; 备份脚本:expdp system/123 ...
- SfMLearner论文笔记——Unsupervised Learning of Depth and Ego-Motion from Video
1. Abstract 提出了一种无监督单目深度估计和相机运动估计的框架 利用视觉合成作为监督信息,使用端到端的方式学习 网络分为两部分(严格意义上是三个) 单目深度估计 多视图姿态估计 解释性网络( ...
- 用xftp从win7系统传输一些必要的文件到Linux
新建会话,主机名为Linux系统的ip地址,选用SFTP协议,选用UTF-8编码格式 1.安装JDK 切换到java路径下 卸载openJDK: 用rpm -qa |grep java指令查看 用rp ...
- S - New Year Transportation
Problem description New Year is coming in Line World! In this world, there are n cells numbered by i ...
- SQLServer In和Exists
In Exists () 1分42秒 5秒 Exists() 返回布尔值 如果子查询结果行>0,则返回 TRUE. 反之返回FALSE exists(select * fro ...
- CDN 内容分发网络
第一步,HTML的文件引用:HTML的文件头(也有文件中,文件尾)那边常有其他文件引用,比如CSS以及JS的引用. 就以bootstrap常用的引用来举个栗子你常见的引用可能会是这样的: <he ...
- Python 遍历目录
代码: 1.递归使用遍历目录 import os def scanfile(path): filelist = os.listdir(path) allfile = [] for filename i ...
- WebGL画点程序v3
本文程序实现画一个点的任务,如下图.其中,点的颜色由Javascript传到片元着色器程序中. 整个程序包含两个文件,分别是: 1. HelloPoint3.html <!DOCTYPE HTM ...
- HDU_1517_博弈(巧妙规律)
A Multiplication Game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- js document 触发按键事件
// 键盘控制 var keyEvent = (function () { document.onkeydown = function (e) { if (e.keyCode === 38) { // ...