leetcode 之Permutation(七)
首先是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(七)的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- LeetCode Palindrome Permutation
原题链接在这里:https://leetcode.com/problems/palindrome-permutation/ 题目: Given a string, determine if a per ...
- 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 ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [leetcode]Next Permutation @ Python
原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: Implement next permutation, which rearra ...
随机推荐
- [AT2557] [arc073_c] Ball Coloring
题目链接 AtCoder:https://arc073.contest.atcoder.jp/tasks/arc073_c 洛谷:https://www.luogu.org/problemnew/sh ...
- [COCI2011-2012#5] POPLOCAVANJE 后缀自动机
题面:洛谷 题解: 其实还可以用AC自动机做,但是没调出来,,,不知道发生了什么... AC自动机做法如下: 观察到如果我们对给定的每个串建AC自动机,那么直接拿大串在上面匹配,如果遇到了一个单词的终 ...
- Android 通过浏览器打开应用
在很多应用的web站,其实都有这样一个功能,就是直接在网页中打开应用,接下来的就来探讨一下这个功能的实现,有些地方也我还没弄明白,还请懂的大神指点. 首先,得说一点不好消息,在微信中,这样的方式是行不 ...
- 十五分钟介绍 Redis数据结构--学习笔记
下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...
- json&pickle序列化
一.用途 我们需要将内存中的数据进行序列化,即写入文件中时,写入的类型只能是字符串或者二进制类型.但是如果我们想要将复杂一些的数据类型,如:列表.字典或者函数之类的同样进行序列化,我们就要用到 jso ...
- linux内核支持nfs挂载配置
1.配置网络部分,主要是使能CONFIG_IP_PNP以在2中能够看到Root file system on NFS选项Networking support Networking options TC ...
- js返回上一页的实现方法
下面是常用代码: <a href="<a href="javascript :history.back(-1)">返回上一页</a> < ...
- python---基础知识回顾(四)(模块sys,os,random,hashlib,re,序列化json和pickle,xml,shutil,configparser,logging,datetime和time,其他)
前提:dir,__all__,help,__doc__,__file__ dir:可以用来查看模块中的所有特性(函数,类,变量等) >>> import copy >>& ...
- Java BLOB 数据的插入与读取 操作
package com.lw.database; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- 从零搭建SSM框架(二)运行工程
启动cnki-manager工程 1.需要在cnki-manager 的pom工程中,配置tomcat插件.启动的端口号,和工程名称. 在cnki-manager的pom文件中添加如下配置: < ...