leetcode_60_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.

Note: Given n will be between 1 and 9 inclusive.

分析:

1、如果使用next_permutation方法实现,会超时

2、本题可采用康托编码实现 (适用范围:没有重复元素的全排列)

简单分析——康托编码(适用范围:没有重复元素的全排列)

适用范围:没有重复元素的全排列

算法:康托编码: 全排列的解码,下面举例说明

如何找出第16个(按字典序的){1,2,3,4,5}的全排列?

1. 首先用16-1得到15

2. 用15去除4! 得到0余15

3. 用15去除3! 得到2余3

4. 用3去除2! 得到1余1

5. 用1去除1! 得到1余0

有0个数比它小的数是1,所以第一位是1

有2个数比它小的数是3,但1已经在之前出现过了所以是4

有1个数比它小的数是2,但1已经在之前出现过了所以是3

有1个数比它小的数是2,但1,3,4都出现过了所以是5

最后一个数只能是2

//方法一:康托编码
class Solution {
public:
    string getPermutation(int n, int k) {
        string str(n, '0'); //必须给初值
        string res(n, '0');
        for(int i = 0; i < n; i++)
            str[i] += i+1; //str.append(i+1);这种方式会报int错

        for(int i = 0; i < n; i++)//依次计算排列的每个位
        {
            int tmp = factorial(str.size() - 1);
            int m = (k - 1) / tmp;
            res[i] = str[m];
            str.erase(m, 1);
            k -= m * tmp;//更新k,其实就是上面m的(k - 1) % tmp;
        }
        return res;
    }

    //求正整数n的阶乘
    int factorial(int n)
    {
        int sum = 1;
        for(int j = 2; j <= n; j++)
            sum *= j;
        return sum;
    }
};
//方法二:封装函数
class Solution {
public:
    string getPermutation(int n, int k) {
        string str(n, '0'); //必须给初值
        string res(n, '0');
        for(int i = 0; i < n; i++)
            str[i] += i+1; //str.append(i+1);这种方式会报int错

        kth_Permutation(n, k, str, res);
        return res;
    }

    string kth_Permutation(int n, int k, string& str, string& res)
    {
        for(int i = 0; i < n; i++)//依次计算排列的每个位
        {
            int tmp = factorial(str.size() - 1);
            int m = (k - 1) / tmp;
            res[i] = str[m];
            str.erase(m, 1);
            k -= m * tmp;//更新k,其实就是上面m的(k - 1) % tmp;
        }
        return res;
    }

    //求正整数n的阶乘
    int factorial(int n)
    {
        int sum = 1;
        for(int j = 2; j <= n; j++)
            sum *= j;
        return sum;
    }
};
时间: 2024-10-10 09:25:09

leetcode_60_Permutation Sequence的相关文章

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq

Acdream 1427 Nice Sequence

Nice Sequence Time Limit: 12000/6000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) Problem Description Let us consider the sequence a1, a2,..., an of non-negative integer numbers. Denote as ci,j the number of occurrences of the number i a

hdu5014 Number Sequence(异或运算)

题目链接: huangjing 题意: 这个题目的意思是给出0~n的排列,然后找出与这个序列的配对使(a0 ⊕ b0) + (a1 ⊕ b1) +·+ (an ⊕ bn)最大.. 思路: 从大到小遍历每个数,然后找到与这个数二进制位数互补的数,那么他们的抑或值必定是pow(2,n)-1,,肯定是最大的.... 题目: Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ