首先是next permutation的算法的描述和分析如下:

这题一是要知道思路,编程中注意STL的用法

 void nextPermutaion(vector<int> &num)
{
next_permutation(num.begin(), num.end());
}
private:
template<typename BidiIt>
bool next_permutation(BidiIt first, BidiIt last)
{
//反向,注意!
auto rfirst = reverse_iterator<BidiIt>(last);
auto rlast = reverse_iterator<BidIt>(first);
auto pivot = next(rfirst);
while (pivot != rlast && *pivot > *prev(pivot))
pivot++; if (pivot == rlast)
{
reverse(rfist, rlast);
return false;
}
//注意用法
auto change = find_if(rfirst, pivot, bind1st(less<int>(), *pivot));
swap(*pivot, *change);
reverse(rfirst, pivot); return true;
}

接着是Permutation Sequence

 

 有人用康托编码来解这个问题,不过个人觉得不太好理解,其实完全可以用上题的思路

string getPermutaion(int n, int k)
{
string s(n, '');
for (int i = ; i < n; i++)
s[i] += i + ; for (int i = ; i < k; i++)
next_permutation(s.begin(), s.end()); return s;
}

leetcode 之Permutation(七)的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  6. LeetCode Palindrome Permutation

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...

  7. Java for LeetCode 060 Permutation Sequence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  9. [leetcode]Next Permutation @ Python

    原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...

随机推荐

  1. Linux网络接口配置文件解析

    [root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0# Intel Corporation 82545EM Gigabit ...

  2. 【loj2461】【2018集训队互测Day 1】完美的队列

    #2461. 「2018 集训队互测 Day 1」完美的队列 传送门: https://loj.ac/problem/2461 题解: 直接做可能一次操作加入队列同时会弹出很多数字,无法维护:一个操作 ...

  3. 洛谷P1062 数列

    题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… (该序列实际上就是 ...

  4. springMVC参数绑定与数据回显

    简单例子:修改商品信息的jsp页面: 参数绑定过程: 1.2.1  默认支持的参数类型 处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值. 1.1.1     HttpServletReq ...

  5. linux命令行设置git提示符

    http://note.youdao.com/noteshare?id=3b6b2ee0925964928fd63d2c51e6bcef

  6. poj 1696 (计算几何基础)

    poj 1696 Space Ant 链接:http://poj.org/problem?id=1696 题意:在坐标轴上,给定n个点的 id 以及点的坐标(xi, yi),让你以最底端点开始,从右依 ...

  7. ubuntu 安装部分设置U盘启动系统安装盘操作

    https://jingyan.baidu.com/article/6079ad0ec4925728ff86db02.html 第三种方法适合新机型,在启动时按F10或F11,可直接选择启动模式 -- ...

  8. JS-DOM-随时更新

    DOM里有三种节点:元素节点.文本节点和属性节点 getElmentById(); //id选择器  在JS中用此方法来查找获取  建议大小写 以免不兼容 有时候查找不到  DOM操作必须等到HTML ...

  9. 【Docker】docker 入门以及一些常用指令

    概述 Docker是一款针对程序开发人员和系统管理员来开发.部署.运行应用的一款虚拟化平台.Docker 可以让你像使用集装箱一样快速的组合成应用,并且可以像运输标准集装箱一样,尽可能的屏蔽代码层面的 ...

  10. Windows API函数大全(精心总结)

    WindowsAPI函数大全(精心总结)    目录 1. API之网络函数... 1 2. API之消息函数... 1 3. API之文件处理函数... 2 4. API之打印函数... 5 5. ...