博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode总结:permutations, permutations II, next permutation, permutation sequence
阅读量:6710 次
发布时间:2019-06-25

本文共 4725 字,大约阅读时间需要 15 分钟。

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的算法:

 

1 class Solution { 2 public: 3     void nextPermutation(vector
&num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 int n=num.size(); 8 if ( n<=1) 9 return;10 int p=n-2;11 while(p>=0&&num[p]>=num[p+1])12 p--;13 if ( p<0 )14 {15 sort(num.begin(),num.end());16 return;17 }18 19 20 int k=p+1;21 while(k
num[p])22 k++;23 k--;24 swap(num[p],num[k]);25 sort(&num[p+1],&num[n]);26 return;27 }28 };

 

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]

 

1 class Solution { 2 public: 3     vector
> permute(vector
&num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 vector
> ans; 8 solve(ans,num,0); 9 return ans;10 }11 12 void solve(vector
>& ans, vector
& num,int k)13 {14 int n =num.size();15 if ( k>=n )16 {17 ans.push_back(num);18 return;19 }20 for(int i=k;i

 

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这个位置过了。

 

 

1 class Solution { 2 public: 3     vector
> permute(vector
&num) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 7 vector
> ans; 8 solve(ans,num,0); 9 return ans;10 }11 12 void solve(vector
>& ans, vector
& num,int k)13 {14 int n =num.size();15 if ( k>=n )16 {17 ans.push_back(num);18 return;19 }20 set
used;21 for(int i=k;i

 

 

 

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.

 

1     string getPermutation(int n, int k) { 2         // IMPORTANT: Please reset any member data you declared, as 3         // the same Solution instance will be reused for each test case. 4         vector
fac(n+1, 1); 5 vector
nums; 6 nums.push_back(0); 7 int i; 8 for(i = 1; i <= n; i++){ 9 fac[i] = fac[i-1]*i;10 nums.push_back(i);11 }12 string res = "";13 k--;14 while(n > 0){15 if(k == fac[n]){16 for(vector
::iterator it = nums.end()-1; it > nums.begin(); it--){17 res += ('0' + *it);18 }19 break;20 }21 else if(k < fac[n-1]){22 res += ('0' + nums[1]);23 nums.erase(nums.begin()+1);24 n--;25 }26 else{27 i = k/fac[n-1];28 k = k%fac[n-1];29 res += ('0' + nums[1+i]);30 nums.erase(nums.begin()+1+i);31 n--;32 }33 }34 return res;35 }

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

 。

转载于:https://www.cnblogs.com/waruzhi/p/3552214.html

你可能感兴趣的文章
HBase设计规范(转载)
查看>>
基础设施即服务系列:在Windows Azure虚拟机上运行SQL Server
查看>>
Android的七巧板Activity之二 Activity的加载模式
查看>>
[技巧]使用Xcode集成的HeaderDoc自动生成注释和开发文档
查看>>
linux下转换U盘文件系统
查看>>
Android应用程序键盘(Keyboard)消息处理机制分析(10)
查看>>
Silverlight/Windows8/WPF/WP7/HTML5周学习导读(7月9日-7月15日)
查看>>
SSD盘阵,技术成熟了吗?
查看>>
北京市定额发票真假查询地址
查看>>
演示:使用IPsec+PKI来完成IP通信的安全
查看>>
“赋能开发者”高峰论坛暨葡萄城联合龙头企业共建模板库正式启动
查看>>
SCCM2012系列之九,SCCM代理安装
查看>>
Exchange Server 2016管理系列课件09.删除和恢复已删除的邮箱
查看>>
Exchange 2013多租户托管PART 4:邮箱隔离管理配置
查看>>
课程所用软件下载地址
查看>>
Mary Meeker最新互联网趋势报告关键词:重写改变一切、轻资产时代
查看>>
KNN算法的實現
查看>>
HDU 1245 Saving James Bond
查看>>
SpringSecurity3整合CAS实现单点登录
查看>>
淘宝网架构分享总结[转]
查看>>