lintcode:Permutations

Given a list of numbers, return all possible permutations.

Challenge
Do it without recursion.

1.递归

class Solution {
public:
    /**
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    vector<vector<int> > res;

    void dfs(vector<int> &nums,vector<int> &sub,int cur){
        if(cur==nums.size()){
            res.push_back(sub);
            return;
        }

        for(int i=0;i<nums.size();i++){
            bool ok=true;
            for(int j=0;j<cur;j++){
                if(nums[i]==sub[j]){
                    ok=false;
                }
            }
            if(ok){
                sub[cur]=nums[i];
                dfs(nums,sub,cur+1);
            }
        }

    }

    vector<vector<int> > permute(vector<int> nums) {
        // write your code here
        int len=nums.size();

        if(len==0){
            return res;
        }

        vector<int> sub(len);

        dfs(nums,sub,0);

        return res;
    }
};

2.非递归

关于求下一个排列见以前的一篇博文 lintcode:Next Permutation

class Solution {
public:
    /**
     * @param nums: A list of integers.
     * @return: A list of permutations.
     */
    void nextPermutation(vector<int> &nums){

        int len=nums.size();

        if(len==1){
            return;
        }

        int partitionIndex;
        int in=len-1;
        while(in){
            if(nums[in-1]<nums[in]){
                partitionIndex=in-1;
                break;
            }
            in--;
        }

        //注意这里是in==0而不是partitionIndex==0;
        //如果in==0,如果nums整个都是逆序的
        if(in==0){
            reverse(nums.begin(),nums.end());
        }else{
            int changeIndex;
            for(int i=len-1;i>partitionIndex;i--){
                if(nums[i]>nums[partitionIndex]){
                   changeIndex=i;
                   break;
                }
            }

            swap(nums[partitionIndex],nums[changeIndex]);
            reverse(nums.begin()+partitionIndex+1,nums.end());
        }

    }

    int factorial(int n){
        return (n==1||n==0)?1:factorial(n-1)*n;
    }

    vector<vector<int> > permute(vector<int> nums) {
        // write your code here
        vector<vector<int> > res;
        if(nums.size()==0){
            return res;
        }

        int sum=factorial(nums.size());

        res.push_back(nums);

        for(int i=1;i<sum;i++){
            nextPermutation(nums);
            res.push_back(nums);
        }

        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: permutations是Python中的一个函数,用于生成一个可迭代对象,其中包含给定序列中所有可能的排列组合。它的语法如下: permutations(iterable, r=None) 其中,iterable是要进行排列组合的序列,r是要生成的排列组合的长度,默认为None,表示生成所有可能的排列组合。 例如,对于序列[1,2,3],可以使用permutations函数生成所有可能的排列组合: from itertools import permutations lst = [1, 2, 3] for p in permutations(lst): print(p) 输出结果为: (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1) 可以看到,permutations函数生成了给定序列中所有可能的排列组合。 ### 回答2: 在Python中,permutations是一个内置函数,它被用于生成一系列的排列,并且可以接受一个可迭代序列作为输入参数。它的基本语法如下: permutations(iterable[, r]) 其中,iterable指定了要被生成排列的对象,它可以是列表、元组、字符串等等;r指定了每个排列的元素个数,默认为None,表示排列元素的个数和输入参数的长度相同。例如: from itertools import permutations # 生成列表的排列 lst = [1, 2, 3] perms = permutations(lst) print(list(perms)) # 输出:[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] # 生成字符串的排列 s = "abc" perms = permutations(s) print(list(perms)) # 输出:[('a', 'b', 'c'), ('a', 'c', 'b'), ('b', 'a', 'c'), ('b', 'c', 'a'), ('c', 'a', 'b'), ('c', 'b', 'a')] 根据上面的例子,我们可以看到,permutations函数会生成可迭代对象,我们需要使用list函数将其转换为列表进行输出。需要注意的是,permutations函数返回的是元组类型,如果需要转换为其他类型,需要使用对应的转换函数或语句。 另外,当指定r参数时,permutations函数会生成长度为r的排列,例如: lst = [1, 2, 3] perms = permutations(lst, 2) print(list(perms)) # 输出:[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] 例如上面的代码会生成lst中任意两个元素的排列。需要注意的是,当指定r参数时,permutations函数会把重复的元素当做不同的元素处理,因此需要注意去重的问题。如果需要去重,可以使用set函数将其转换为集合类型,或者使用ordered_set库进行去重操作。 总之,permutations是一个非常有用的工具,可以用于生成排列,进行组合等等高级操作,同时由于其是内置的Python函数,使用也非常便捷。 ### 回答3: Python中的permutations是一个用于生成给定列表中的所有可能排列的函数。这个函数可以用于得到一个可遍历对象(iterable),其包含了原列表中所有元素的所有可能排列。 permutations函数的语法如下:itertools.permutations(iterable, r=None)。其中iterable是指要进行排列的元素,r表示每个排列的固定长度,如果不指定,则默认为iterable的长度。 接下来我们将介绍一些关于permutations函数的常见用法: ### 生成所有排列 ``` from itertools import permutations a = [1,2,3] p = permutations(a) for i in p: print(i) ``` 这段代码将输出a列表中元素的所有排列方式,即: ``` (1, 2, 3) (1, 3, 2) (2, 1, 3) (2, 3, 1) (3, 1, 2) (3, 2, 1) ``` ### 指定每个排列的长度 ``` from itertools import permutations a = [1,2,3] p = permutations(a, 2) for i in p: print(i) ``` 这段代码将输出长度为2的所有排列方式,即: ``` (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2) ``` ### 对字符串进行排列 ``` from itertools import permutations a = "abc" p = permutations(a) for i in p: print(i) ``` 这段代码将输出字符串“abc”的所有排列方式,即: ``` ('a', 'b', 'c') ('a', 'c', 'b') ('b', 'a', 'c') ('b', 'c', 'a') ('c', 'a', 'b') ('c', 'b', 'a') ``` 通过permutations函数的使用,我们可以快速地生成所有可能的排列,可以应用于数学、密码学、电子商务等领域中的问题。需要注意的是,由于permutations函数的计算量较大,同时生成的排列次序是随机的,因此在实际应用时需要考虑时间复杂度以及随机性的影响。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值