美文网首页
lintcode 15 全排列

lintcode 15 全排列

作者: 小雨启明 | 来源:发表于2018-11-04 23:28 被阅读0次
class Solution {
public:
    /*
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    vector<vector<int>> permute(vector<int> &nums) {
        // write your code here
        vector<vector<int>> res;
        if(nums.size() < 2)
        {
            res.push_back(nums);
            return res;
        }
        help(res,0,nums.size(),nums);
        return res;
    }
    
    void help(vector<vector<int>>& res,int begin,int end,vector<int> &nums)//得到从begin开始的全排列
    {
        if(begin == end){                      //基本情况  
            res.push_back(nums);             
        }else{
            for(int i = begin;i < end;i++){//遍历整个数组
                swap(nums[begin],nums[i]);//把被遍历的数放在首位
                help(res,begin+1,end,nums);//求首位之后的所有排列情况
                swap(nums[begin],nums[i]);//恢复原始数组
            }
        }
    }
    void swap(int& a,int& b){
        int temp = a;
        a = b;
        b = temp;
    }
};

递归算法思路:对数据[1,2,3],需要一次遍历;每次遍历,确定首位,[1,...];[2,...];[3,...]然后通过递归得到后面的所有排列情况,对于数组中有重复元素的情况,我们只要保证,重复元素只能有一次作为子问题的开头元素,这样我们就可以避免重复计算。

非递归算法思路:https://www.cnblogs.com/answeryi/archive/2011/10/12/2209058.html
字典排序 注意 逆序的步骤不要错过

class Solution {
public:
    /*
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    vector<vector<int>> permute(vector<int> &nums) {
        // write your code here
        vector<vector<int>> res;
        if(nums.size() < 2){
            res.push_back(nums);
            return res;
        }
        sort(nums.begin(),nums.end());
        int point = -1;
        res.push_back(nums);
        while((point = findpoint(nums)) != -1){
            int next = findnext(nums,point);
            swap(nums[point-1],nums[next]);
            diandao(nums,point);
            res.push_back(nums);
        }
        return res;
    }
    int findpoint(vector<int> &nums){
        int len =  nums.size();
        for(int i = len-1;i >0;i--){
            if(nums[i] > nums[i-1])
                return i;
        }
        return -1;
    }
    int findnext(vector<int> &nums,int point){
        int len  = nums.size();
        int i  = 0;
        for(i = point; i < len;i++){
            if(nums[point-1] > nums[i]){
                return i-1;
            }
        }
        return i-1;
    }
    void  diandao(vector<int> &nums,int point){
        int j = nums.size()-1;
        int i = point;
        while(i < j){
            swap(nums[i],nums[j]);
            i++;
            j--;
        }
    }
    void swap(int& a,int& b){
        int temp;
        temp = a;
        a = b;
        b = temp;
    }
};

相关文章

  • lintcode 15 全排列

    递归算法思路:对数据[1,2,3],需要一次遍历;每次遍历,确定首位,[1,...];[2,...];[3,......

  • lintcode 全排列

    给定一个数字列表,返回其所有可能的排列,第十五题是没有重复的数字,是十六题是有重复的数字。先来说没有重复数字的情况...

  • 全排列 (lintcode:permutations)

    给定一个数字列表,返回其所有可能的排列。假设没有重复数字。样例:给出一个列表[1,2,3],其全排列为: 代码: ...

  • LintCode全排列的非递归写法

    题目点这里花了好久写的(没有IDE,只能靠println来手动debug了)不过算法算是码农的基本功,还是需要多多...

  • 全排列与字典序

    全排列 递归实现全排列; 首先来说递归算法实现全排列: 例如,对于{1,2,3,4}的例子进行全排列,其可以分解...

  • 全排列

    求全排列最简单的就是递归了123 的全排列共有 6 个, 123 的全排列等于以 1 开头 23 的全排列, 加上...

  • 全排列

    题目 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排...

  • 全排列

    递归的版本image.png

  • 全排列

  • 全排列

网友评论

      本文标题:lintcode 15 全排列

      本文链接:https://www.haomeiwen.com/subject/yqndxqtx.html