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. SQL - 生成指定范围内的随机数

    今天按照公司需求,需要做一个sql作业来对数据库定时触发,其中有个难点,就是在sql中需要在1-n中随机出来一个结果. google了半天,找到一个比较好的方式. 写下这个sql: DECLARE @ ...

  2. CMD命令名详细大全

    在运行菜单里键入CMD,就可以调出CMD命令窗口,有关某个命令的详细信息,请键入 HELP 命令名 ASSOC 显示或修改文件扩展名关联. AT 计划在计算机上运行的命令和程序.ATTRIB 显示或更 ...

  3. js地理位置获取、显示、轨迹绘制

    JS新API标准 地理定位(navigator.geolocation) 基于 html5 geolocation来获取经纬度地址 Html5 Geolocation获取地理位置信息 HTML5获取地 ...

  4. fastCGI与PHP-fpm

    fastCGI是nginx和php之间的一个通信接口,该接口实际处理过程通过启动php-fpm进程来解析php脚本,即php-fpm相当于一个动态应用服务器,从而实现nginx动态解析php.因此,如 ...

  5. MYSQL界面操作系统之phpMyAdmin

    linux下: 需要PHP环境支持,安装PHP自行百度 下载linux-phpMyAdmin,并解压 php -S 127.0.0.1:8081 -t phpMyAdmin/

  6. cut

    cut是一个针对行的数据选取命令 SYNOPSIS cut [OPTION]... [FILE]... OPTION -b 以字节为单位进行分割,如果是多字节的话就需要注意了 -c 以字符为单位进行分 ...

  7. Hive UDF 实验1

    项目中使用的hive版本低于0.11,无法使用hive在0.11中新加的开窗分析函数. 在项目中需要使用到row_number()函数的地方,有人写了udf来实现这个功能. new java proj ...

  8. Solr字段配置错误

    在站内搜索Solr Schema设计时,有个FTS_URL字段(之前设计url也会参与检索和打分),因此其配置信息如下: <field name="FTS_URL" type ...

  9. NOIP2010普及组T4 三国游戏——S.B.S.

    题目描述 小涵很喜欢电脑游戏,这些天他正在玩一个叫做<三国>的游戏. 在游戏中,小涵和计算机各执一方,组建各自的军队进行对战.游戏中共有 N 位武将(N为偶数且不小于 4),任意两个武将之 ...

  10. 单调队列应用--BZOJ 3831 Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec  Memory Limit: 128 MB Description In the Byteotian Lin ...