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,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

用的是STL里面rotate的算法:

Permutations:

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1]

 class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function vector<vector<int> > ans;
solve(ans,num,);
return ans;
} void solve(vector<vector<int> >& ans, vector<int>& num,int k)
{
int n =num.size();
if ( k>=n )
{
ans.push_back(num);
return;
}
for(int i=k;i<n;i++)
{
swap(num[i],num[k]);
solve(ans,num,k+);
swap(num[i],num[k]);
}
}
};

Permutations II:

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2][1,2,1], and [2,1,1].

这回是说不能要重复的。

其实如果可以用STL的函数next_permutation的话,一直next_permutation到false,每次把num加到结果里就行了,很简单吧~当然要先sort一下,不过面试时这样答的话,面试官就会让你实现next_permutation了~好在我们前面也做过啊~

另外一个方法就是用一个set记录num[i]是否已经放在K这个位置过了。

Permutation Sequence:

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

     string getPermutation(int n, int k) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<int> fac(n+, );
vector<int> nums;
nums.push_back();
int i;
for(i = ; i <= n; i++){
fac[i] = fac[i-]*i;
nums.push_back(i);
}
string res = "";
k--;
while(n > ){
if(k == fac[n]){
for(vector<int>::iterator it = nums.end()-; it > nums.begin(); it--){
res += ('' + *it);
}
break;
}
else if(k < fac[n-]){
res += ('' + nums[]);
nums.erase(nums.begin()+);
n--;
}
else{
i = k/fac[n-];
k = k%fac[n-];
res += ('' + nums[+i]);
nums.erase(nums.begin()++i);
n--;
}
}
return res;
}

如果题目反转,是已知字符串,求是第几个,则可以用公式∑k*m!(m从0到n,k表示第m+1位之后有多少小于第m+1位个数)

leetcode总结:permutations, permutations II, next permutation, permutation sequence的更多相关文章

  1. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  2. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  3. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  4. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  5. 【LeetCode】46. Permutations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...

  6. LeetCode Single Number I / II / III

    [1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...

  7. [array] leetcode - 40. Combination Sum II - Medium

    leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...

  8. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  9. LeetCode:路径总和II【113】

    LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...

  10. LeetCode:组合总数II【40】

    LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...

随机推荐

  1. 搭建openvpn 未完成。。。

    轻松构建自己的OpenVPN家庭服务器(VMware+Amahi) http://os.51cto.com/art/201107/277146_all.htm  这是教程 不用安装第一步的,直接把下载 ...

  2. 《Swift开发指南》

    <Swift开发指南> 基本信息 作者: 关东升    赵志荣 丛书名: 图灵原创 出版社:人民邮电出版社 ISBN:9787115366245 上架时间:2014-8-5 出版日期:20 ...

  3. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  4. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. html不使用cache数据

    <HEAD>      <META   HTTP-EQUIV="Pragma"   CONTENT="no-cache">     &l ...

  6. Mongodb--gridfs与分片实验

    1.放置一个大文件到gridfs,查看fs.chunks和fs.files的情况. Step1.开启一台mongod服务. ./mongod --dbpath dbs/master     登录mon ...

  7. HFSS学习(一)计划

    2015-11-28 21:05:33 基本概念 边界条件 激励源 建模 网格划分 变量设置与调谐优化 仿真结果 实例 微带线仿真 Ku波段微带线发夹线滤波器仿真 介质滤波器 腔体滤波器 微带一分四功 ...

  8. uva 839 not so mobile——yhx

    Not so Mobile  Before being an ubiquous communications gadget, a mobile was just a structure made of ...

  9. Linux与Windows 解压乱码 UTF8BOM读取问题

    Linux 与 Windows 文件乱码问题 这几天需要在linux下用CNN跑数据,但是把数据和数据列表list上传到linux下时却出现了不少乱码的问题.将这两天碰到的编码问题简单的总结一下. 1 ...

  10. 矩阵乘法快速幂 codevs 1732 Fibonacci数列 2

    1732 Fibonacci数列 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description 在“ ...