LintCode : Permutations II

import java.util.Collections;
class Solution {
    /**
     * @param nums: A list of integers.
     * @return: A list of unique permutations.
     */
    public ArrayList<ArrayList<Integer>> permuteUnique(ArrayList<Integer> nums){
        // write your code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if (nums == null || nums.size() == 0) {
            return result;
        }
        Collections.sort(nums);
        boolean[] visited = new boolean[nums.size()];
        ArrayList<Integer> list = new ArrayList<Integer>();
        permutationHelper(result, list, nums, visited);
        return result;
    }
    
    private void permutationHelper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, ArrayList<Integer> nums, boolean[] visited) {
        if (list.size() == nums.size()) {
            result.add(new ArrayList<Integer>(list));
            return;
        }
        
        for (int i = 0; i < nums.size(); i++) {
            if (visited[i] == true || (i != 0 && nums.get(i) == nums.get(i - 1) && visited[i - 1] == false)) {
                continue;
            }
            visited[i] = true;
            list.add(nums.get(i));
            permutationHelper(result, list, nums, visited);
            list.remove(list.size() - 1);
            visited[i] = false;
        }
    }
}

  用visited数组来记录哪些是访问过的,visited[i - 1] == false很tricky,可能存在1222......这种情况,用第二个2递归的时候,前面那个2应该是还没有访问的,所以这个时候visited[i - 1] == false。

转载于:https://www.cnblogs.com/dingjunnan/p/5205904.html

  • 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、付费专栏及课程。

余额充值