首页 > 其他分享 >lintcode:Permutations

lintcode:Permutations

时间:2022-12-01 19:04:40浏览次数:46  
标签:return sub nums int res Permutations lintcode vector


Given a list of numbers, return all possible permutations.

Challenge
Do it without recursion.

1.递归

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > res;

void dfs(vector<int> &nums,vector<int> &sub,int cur){
if(cur==nums.size()){
res.push_back(sub);
return;
}

for(int i=0;i<nums.size();i++){
bool ok=true;
for(int j=0;j<cur;j++){
if(nums[i]==sub[j]){
ok=false;
}
}
if(ok){
sub[cur]=nums[i];
dfs(nums,sub,cur+1);
}
}

}

vector<vector<int> > permute(vector<int> nums) {
// write your code here
int len=nums.size();

if(len==0){
return res;
}

vector<int> sub(len);

dfs(nums,sub,0);

return res;
}
};

2.非递归

关于求下一个排列见以前的一篇博文 ​​lintcode:Next Permutation​​

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
void nextPermutation(vector<int> &nums){

int len=nums.size();

if(len==1){
return;
}

int partitionIndex;
int in=len-1;
while(in){
if(nums[in-1]<nums[in]){
partitionIndex=in-1;
break;
}
in--;
}

//注意这里是in==0而不是partitionIndex==0;
//如果in==0,如果nums整个都是逆序的
if(in==0){
reverse(nums.begin(),nums.end());
}else{
int changeIndex;
for(int i=len-1;i>partitionIndex;i--){
if(nums[i]>nums[partitionIndex]){
changeIndex=i;
break;
}
}

swap(nums[partitionIndex],nums[changeIndex]);
reverse(nums.begin()+partitionIndex+1,nums.end());
}

}

int factorial(int n){
return (n==1||n==0)?1:factorial(n-1)*n;
}

vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int> > res;
if(nums.size()==0){
return res;
}

int sum=factorial(nums.size());

res.push_back(nums);

for(int i=1;i<sum;i++){
nextPermutation(nums);
res.push_back(nums);
}

return res;
}
};


标签:return,sub,nums,int,res,Permutations,lintcode,vector
From: https://blog.51cto.com/u_15899184/5903609

相关文章

  • lintcode:Subsets
    Givenasetofdistinctintegers,returnallpossiblesubsets.ChallengeCanyoudoitinbothrecursivelyanditeratively?1.18sclassSolution{public:/**......
  • lintcode: Permutations II
    Givenalistofnumberswithduplicatenumberinit.Findalluniquepermutations.可以见我的博文​​全排列实现​​classSolution{public:/***@paramnu......
  • lintcode: Subsets II
    Givenalistofnumbersthatmayhasduplicatenumbers,returnallpossiblesubsets1.先排序;再按求Subsets一样的做法,只是添加前检查是否已经存在。耗时171mscla......
  • lintcode:Subarray Sum Closest
    Givenanintegerarray,findasubarraywithsumclosesttozero.Returntheindexesofthefirstnumberandlastnumber.ExampleGiven[-3,1,1,-3,5],retur......
  • lintcode: Sqrt(x)
    Implementintsqrt(intx).Computeandreturnthesquarerootofx.Examplesqrt(3)=1sqrt(4)=2sqrt(5)=2sqrt(10)=3ChallengeO(log(x))求非线性方程的解可以......
  • Codeforces - 1391C - Cyclic Permutations(思维 + 组合数学 + 数论 + 图论、*1500)
    1391C-CyclicPermutations(⇔源地址)目录1391C-CyclicPermutations(⇔源地址)tag题意思路AC代码错误次数:0tag⇔思维、⇔组合数学、⇔数论、⇔......
  • SP15637 GNYR04H - Mr Youngs Picture Permutations
    SP15637GNYR04H-MrYoungsPicturePermutations-洛谷|计算机科学教育新生态(luogu.com.cn)好题。考虑从小到大(身高从高到低)安排每个数的位置。这样,已经被安排......
  • POJ 1825/2279(Young/Mr. Young's Picture Permutations-杨氏矩阵和钩子公式)
    给出一个n行的矩阵,每一行有a[i]个数,总共有sum个数,要求每一个位置的数必须比上面的数和左面的数大,求总方案数.杨氏矩阵又叫杨氏图表,它是这样一个矩阵,满足条件:(1)如果格子......
  • CF 1677D(Tokitsukaze and Permutations-冒泡排序)
    已知长度为n的排列,经过k次冒泡(每次把最大的数交换到最后)后,得到的新序列为.现在已知的某些地方的值,不知道的记,求合法原排列数。考虑和排列达成双射关系。且1次冒泡会导致......
  • TEMPLATE3. Permutations 排列
    TEMPLATE3.Permutations组合,可以乱序。所以,需要记录哪些数用过了。每次递归时,选用第一个没用过的数。注意回溯时清空标记。//TEMPLATE3.Permutations#include<bits......