求一个排列的下一个排列。

1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; class Solution{
public:
void nextPermutation(vector<int> &num) {
if(num.size() == )
return; const vector<int>::reverse_iterator rfirst= num.rbegin();
const vector<int>::reverse_iterator rend = num.rend(); auto pivot = next(rfirst);
while(pivot != rend && *pivot >= *prev(pivot))
{
++pivot;
} if(pivot == rend)
{
reverse(rfirst,rend);
return;
}
//find the first num great than pivot
auto change = rfirst;
while(*change<=*pivot)
++change; swap(*change,*pivot);
reverse(rfirst,pivot);
return;
}
}; int main()
{
vector<int> num;
num.push_back();
num.push_back();
num.push_back(); Solution myS;
myS.nextPermutation(num);
return ;
}

LeetCode OJ--Next Permutation *的更多相关文章

  1. LeetCode OJ 60. Permutation Sequence

    题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of th ...

  2. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  3. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  4. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  5. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  6. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. [array] leetcode - 31. Next Permutation - Medium

    leetcode - 31. Next Permutation - Medium descrition Implement next permutation, which rearranges num ...

  8. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  9. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  10. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

随机推荐

  1. Unity基础-脚本的优化

    脚本的优化 object pool 避免频繁的内存分配和gc噩梦(字符串相加?) 是否有必要都写在update里?分帧? 需要的只取一次 使用editor内赋值,而不是find 复杂的物理 复杂的数学 ...

  2. vsftpd服务安装与虚拟用户配置

    vsftpd的全名是“Very secure FTP Daemon” 一.安装vsftpd安装db4-util用于生成认证文件 yum -y install db4-utils 安装vsftpd yu ...

  3. Python基础——数值

    运算 运算 运算符 示例 加 + 减 - 乘 * 除 / 取余 % 幂 ** 赋值 = 绝对值 abs 取整(四舍五入) round 最大值 max 最小值 min 科学计数法 e 十六进制 0x 逻 ...

  4. leetcode-23-DynamicProgramming-1

    357. Count Numbers with Unique Digits 解题思路: 用arr[i]存放长度为i时,各位互不相同的数字的个数,所以arr[1]=10,arr[2]=9*9.(第一位要 ...

  5. ProC第二弹

    一.提要 上文简单介绍了Windows下ProC配置开发,这次我们使用Linux平台再次配置Oracle ProC开发环境(RedHat Linux 9 + Oracle 92).    <OR ...

  6. POJ:1751-Highways(Kruskal和Prim)

    Highways Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6078 Accepted: 1650 Special Judg ...

  7. 如何用ADMINISTRATOR登陆WIN7

    Windows 7系统出于安全考虑,将系统超级管理员帐户(Administrator)隐藏了,不允许"普通用户"使用.很多时候特别是安装一些应用软件时,由于兼容的问题,普通权限的用 ...

  8. js中xml文件加载

  9. 控制台窗口和powershell运行服务会卡住的解决办法

    之前使用nodejs做了一个简单的web服务,通过控制台窗口运行,通过浏览器访问发现有时候浏览器等很久数据都加载不出来,以为是代码有问题,后来发现是控制台卡住了,按一下enter键就好了,当时百度了一 ...

  10. Mac 之 STF 搭建(淘宝源安装)

    参考链接:https://www.jianshu.com/p/5fe8cb7d214f (MAC直接安装STF)https://www.jianshu.com/p/c5c298486dbd(homeb ...