Next Permutation

备忘

/*
date:
    2018/3/8 10:53
des:
    2.1.12 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,31,3,2
    3,2,11,2,3
    1,1,51,5,1
analyze:
    先看一下题目,permutation(排列)给出一个数列,输出下一个全排列比当前数列大的最小的排列,如果当前是最大就输出最小的那个.
    不分配额外的空间

    那么问题来了如何生成全排列
*/

#include "type.h"

int arr_permutation[] = {1,2,3,4};
int arr_count = sizeof(arr_permutation)/sizeof(int);
vectorInt nums_permutation(arr_permutation,arr_permutation+arr_count);

template<typename InIt>
void Print(InIt begin,InIt end)
{
    while(begin!=end)
    {
        printf("[%d]",*begin);
        begin++;
    }
    printf("\n");
}

//打印全排列 递归实现
template<typename Source,typename SourceIt>
void permunationGenerationRecursion(Source &nums, SourceIt begin, SourceIt end)
{
    //Print(begin,end);
    if (begin==end)
    {
        Print(nums.begin(),nums.end());
        return;
    }
    for (SourceIt it=begin; it!=end; it++)
    {
        swap(*it,*begin);//a{} 交换a 
        permunationGenerationRecursion(nums,begin+1,end);
        swap(*it,*begin);//将数组恢复
    }
}
//非递归实现
void permunationGeneration(vectorInt &nums)
{
    sort(nums.begin(),nums.end());//先排序
    while(true)
    {
        int index = 0;
        for(int j=nums.size()-1; j!=0; j--)
        {
            if (nums[j]>nums[j-1])
            {
                //存在下一个排列
                index = j-1;
                break;
            }
        }
        if (index<0)
        {
            return;//已经输出全部排列
        }
        //寻找交换的位置
        for (int i=nums.size()-1; i!=index; i--)
        {
            if (nums[i]>nums[index])
            {
                swap(nums[i],nums[index]);
                reverse(nums.begin()+index+1,nums.end());
                Print(nums.begin(),nums.end());//输出
                break;
            }
        }
    }

}

int nextPermunation(vectorInt&nums)
{
    vectorInt::reverse_iterator rightIt = nums.rbegin();
    vectorInt::iterator beginIt = nums.begin();
    int minValue = 0;
    while((rightIt+1) != nums.rend())
    {
        int nextVal = *(rightIt+1);
        if (nextVal<*rightIt)
            break;
        rightIt++;
    }

    if ((rightIt+1) == nums.rend())
    {
        reverse(nums.begin(),nums.end());
    }
    else
    {
        vectorInt::reverse_iterator it = nums.rbegin();
        while(it != rightIt+1)
        {
            if (*it>*(rightIt+1))
            {
                swap(*it,*(rightIt+1));
                reverse(nums.rbegin(),rightIt+1);
                break;
            }
            it++;
        }
    }
    return 0;
}


参考
http://blog.csdn.net/e3399/article/details/7543861

https://www.cnblogs.com/nowornever-L/p/6008954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值