zoukankan      html  css  js  c++  java
  • 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的算法:

     1 class Solution {
     2 public:
     3     void nextPermutation(vector<int> &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<n&&num[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<vector<int> > permute(vector<int> &num) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         
     7         vector<vector<int> > ans;
     8         solve(ans,num,0);
     9         return ans;
    10     }
    11 
    12     void solve(vector<vector<int> >& ans, vector<int>& 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<n;i++)
    21         {
    22             swap(num[i],num[k]);
    23             solve(ans,num,k+1);
    24             swap(num[i],num[k]);
    25         }
    26     }
    27 };

    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<vector<int> > permute(vector<int> &num) {
     4         // Start typing your C/C++ solution below
     5         // DO NOT write int main() function
     6         
     7         vector<vector<int> > ans;
     8         solve(ans,num,0);
     9         return ans;
    10     }
    11 
    12     void solve(vector<vector<int> >& ans, vector<int>& num,int k)
    13     {
    14         int n =num.size();
    15         if ( k>=n )
    16         {
    17             ans.push_back(num);
    18             return;
    19         }
    20             set<int> used;
    21         for(int i=k;i<n;i++)
    22         {
    23                     if(used.find(num[i]) != used.end())
    24                         continue;
    25             swap(num[i],num[k]);
    26             solve(ans,num,k+1);
    27             swap(num[i],num[k]);
    28                     used.insert(num[i]);
    29         }
    30     }
    31 };
    32         

    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<int> fac(n+1, 1);
     5         vector<int> 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<int>::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位个数)

     。

  • 相关阅读:
    ubuntu安装gradle
    ubuntu文件查找
    接口服务flask的负载均衡部署
    区块链节点网络的nginx转发部署
    typescript检查包之间是否有循环依赖
    shiro对事务的影响
    防止xss(脚本攻击)的方法之过滤器
    Mysql批量更新速度慢的解决方案
    springBoot的三种启动方式
    mybatis的注解开发之三种动态sql
  • 原文地址:https://www.cnblogs.com/waruzhi/p/3552214.html
Copyright © 2011-2022 走看看