leetcode4:Permutation

#include <utility>
#include <iostream>
#include <vector>
#include <algorithm>
//the next permutation
template<class BidirIt>
bool STL_next_permutation(BidirIt first, BidirIt last)
{
    if (first == last) return false;
    BidirIt i = last;
    if (first == --i) return false;

    while (1) {
        BidirIt i1, i2;

        i1 = i;
        --i;
        if (*i < *i1) {
            i2 = last;
            while (!(*i < *--i2))
                ;
            std::iter_swap(i, i2);
            std::reverse(i1, last);
            return true;
        }
        if (i == first) {
            std::reverse(first, last);
            return false;
        }
    }
}
void nextPermutation(int A[],int len)
{
    STL_next_permutation(A, A+len);
}

//full pemutation
void fullPerm(int A[],int m,int n)
{
    if(m == n)
    {
        for(int i=0;i<n+1;i++)
            std::cout << A[i] << " ";
        std::cout << std::endl;
        return;
    }
    else
    {
        for(int i=m;i<n+1;i++)
        {
            std::swap(A[m], A[i]);
            fullPerm(A,m+1,n);
            std::swap(A[m], A[i]);
        }
    }
}

int Factorial(int n)
{
    int fac=1;
    for(int i=1;i<=n;i++)
    {
        fac *=i;
    }
    return fac;
}
//康托编码第k个序列
void CantorCode(int A[],int len,int k)
{
    --k;
    std::vector<std::pair<int,bool>> v;
    for(int i=0;i<len;i++)
    {
        v.emplace_back(A[i],false);
    }

    for(int i=0;i<len;i++)
    {
        int j;
        int t=k/Factorial(len-i-1);
        for(j=0;j<len;j++)
        {
            if(!v[j].second)
            {
                if(t==0) break;
                --t;
            }
        }
        A[i]=v[j].first;
        v[j].second=true;
        k=k%Factorial(len-i-1);
    }
}
时间: 2024-07-31 14:29:41

leetcode4:Permutation的相关文章

31. 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 replaceme

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): "123" "132" "213" "231" "312" "3

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 replaceme

LeetCode 31. Next Permutation

Problem: https://leetcode.com/problems/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 poss

60. 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): "123" "132" "213" "231" "312" "3

【数组】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 repla

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/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

HDU3664 Permutation Counting

Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1487    Accepted Submission(s): 754 Problem Description Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-val

UVALive 6508 Permutation Graphs

Permutation Graphs Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ID: 650864-bit integer IO format: %lld      Java class name: Main 解题:逆序数 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long l