Permutation Sequence

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.

解题思路,采用标准库的next_premutation()进行枚举到第K个排列。

 

Code:

string getPermutation(int n,int k){

    string(n,'0');

    for(int i=0;i

        s[i]+=i+1;

    }

    for(int i=0;i

        next_permutation(s.begin(),s.end());

    }

    return s;

}

 

你可能感兴趣的:(LeetCode,LeetCode)