leetcode总结:permutations, permutations II, next permutation, permutation sequence
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):
"123"
"132"
"213"
"231"
"312"
"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的更多相关文章
- 【一天一道LeetCode】#47. Permutations II
一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- 【LeetCode】46. Permutations 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 方法一:库函数 方法二:递归 方法三:回溯法 日期 题目地址:h ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- [array] leetcode - 40. Combination Sum II - Medium
leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) an ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- LeetCode:路径总和II【113】
LeetCode:路径总和II[113] 题目描述 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树, ...
- LeetCode:组合总数II【40】
LeetCode:组合总数II[40] 题目描述 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candi ...
随机推荐
- Windows Server 2008 R2怎样设置自动登陆
Windows Server 2008 R2是一款服务器操作系统,提升了虚拟化.系统管理弹性.网络存取方式,以及信息安全等领域的应用,Windows Server 2008 R2也是第一个只提供64位 ...
- 关于zend_parse_parameters函数
PHP_FUNCTION(set_time_limit) { long new_timeout; char *new_timeout_str; int new_timeout_strlen; if ( ...
- window10系统安装oracle11g时遇到INS-13001环境不满足最低要求
机器安装了window10系统,之前有次安装oracle11g是成功了.但是机器后来固态硬盘坏了,又坏了个后,还是win10系统安装oracle11g时,出现INS-13001环境不满足最低要求,郁闷 ...
- 在Myeclipse中配置Maven
第一步:下载maven安装包,配置环境变量M2_HOME;变量值为maven的解压目录. 第二步:在eclipse4.0之前的版本需要安装maven插件,方法即:将maven插件包复制到eclipse ...
- 前端mock数据之mockjax和mockjson
用处 在前后台共同进行一个项目的时候常会遇到一种情景, 后台定义好接口,前端按照接口进行开发, 当前端开发完成后台接口却还没有开发完成, 这个时候要进行接口测试, 只能等后台开发完成才能测试, 在这中 ...
- redis master配置了密码进行主从同步
1.如果master不设置密码,那么直接在slave服务器配置slaveof即可 配置如下 #slaveof ip 端口 slaveof 配置好我们看下redis的日志 看是否同步成功 :S Jan ...
- Python HeapSort
__author__ = 'student' print 'hello world hello python' ''' heap sort root leftchild 2n+1 rightchild ...
- Regarding learning
when you learn something, just like learn computer language. if you just learn some basic usage, not ...
- 对于大学4年的反思(续),记我的ThoughtWorks面试
之前我写了一篇对于大学四年的反思,时隔一个月,为什么我这么快就要来写这篇续章呢?主要有两个原因,第一是感谢静子姐姐,记得知乎上有个回答里面说过人生需要有贵人的帮助,遇到贵人是一件很幸运的事情.我想,静 ...
- UESTC 912 树上的距离 --LCA+RMQ+树状数组
1.易知,树上两点的距离dis[u][v] = D[u]+D[v]-2*D[lca(u,v)] (D为节点到根节点的距离) 2.某条边<u,v>权值一旦改变,将会影响所有以v为根的子树上的 ...