[Lintcode]16. Permutations II/[Leetcode]47. Permutations II

16. Permutations II/47. Permutations II

  • 本题难度: Medium
  • Topic: Search & Recursion

Description

Given a list of numbers with duplicate number in it. Find all unique permutations.

Example

Example 1:

Input: [1,1]

Output:

[

[1,1]]

Example 2:

Input: [1,2,2]

Output:

[

[1,2,2],

[2,1,2],

[2,2,1]]

Challenge

Using recursion to do it is acceptable. If you can do it without recursion, that would be great!

我的代码

法1: 递归 BFS

class Solution:
    """
    @param: :  A list of integers
    @return: A list of unique permutations
    """

    #recursion
    #dfs
    def permuteUnique(self, nums):
        # write your code here
        nums.sort()
        res = []
        n = len(nums)
        self.dfs(nums,[],res,n)
        return res

    def dfs(self,nums,path,res,n):
        if len(path) == n:
            res.append(path)
        for i in range(len(nums)):
            if i>0 and nums[i-1]==nums[i]:
                continue
            self.dfs(nums[:i]+nums[i+1:],path+[nums[i]],res,n)
            

法2 非递归 DFS

    #no recursion
    def permuteUnique(self, nums):
        # write your code here
        res = []
        nums.sort()
        containt = [[[], nums]]
        while (len(containt) > 0):
            [l1, l2] = containt.pop()
            if len(l2) == 0:
                res.append(l1)
            for i in range(len(l2)):
                if i > 0 and l2[i - 1] == l2[i]:
                    continue
                containt.append([l1 + [l2[i]], l2[:i] + l2[i + 1:]])

        return res

法3: 非递归,BFS

#BFS
def permuteUnique(nums):
    # write your code here
    res = []
    nums.sort()
    containt = collections.deque([[[], nums]])
    while (len(containt) > 0):
        [l1, l2] = containt.popleft()
        print(‘l1‘,l1,‘l2‘,l2)
        if len(l2) == 0:
            res.append(l1)
        for i in range(len(l2)):
            if i > 0 and l2[i - 1] == l2[i]:
                continue
            containt.append([l1 + [l2[i]], l2[:i] + l2[i + 1:]])

    return res

别人的代码

均为非递归,但是没看懂,以后再看

1


    def permuteUnique(self, nums):
        ans = [[]]
        for n in nums:
            new_ans = []
            for l in ans:
                for i in range(len(l)+1):
                    new_ans.append(l[:i]+[n]+l[i:])
                    if i<len(l) and l[i]==n: break              #handles duplication
            ans = new_ans
        return ans

2

    def permute(nums):
        permutations = [[]]

        for head in nums:
            permutations = [rest[:i]+[head]+rest[i:] for rest in permutations for i in range(len(rest)+1)]

        return permutations

思路

递归比较好理解,DFS。

用栈是DFS

用队列是BFS

  • 时间复杂度 O(n!)

原文地址:https://www.cnblogs.com/siriusli/p/10386629.html

时间: 2024-10-15 19:49:11

[Lintcode]16. Permutations II/[Leetcode]47. Permutations II的相关文章

[leetcode] 47. 全排列 II

47. 全排列 II 比上一个题多了个重复性 与46. 全排列完全一样的代码... class Solution { // 当没有下一个排列时return false public boolean nextPermutation(int[] nums) { if (nums.length == 1) { return false; } int p = -1; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] < nums[i +

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

19.2.7 [LeetCode 47] Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. Example: Input: [1,1,2] Output: [ [1,1,2], [1,2,1], [2,1,1] ] 1 class Solution { 2 public: 3 vector<vector<int>> permuteUnique(vector<int&

LeetCode 47 Permutations II(全排列)

题目链接: https://leetcode.com/problems/permutations-ii/?tab=Description 给出数组,数组中的元素可能有重复,求出所有的全排列 使用递归算法: 传递参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列组合 nums保存初始的数组 used随着计

LeetCode 47. 全排列 II(Permutations II)

题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路 类似于LeetCode46.全排列,只不过对于每个起始位置维护一个之前遍历过的数集,在交换时判断当前数是否在数集中出现,若出现过则不能交换此数,否则与起始位置交换并将此数加入到数集中. 代码 1 class Solution { 2 public: 3 vector<vector<int>> permuteU

LeetCode 47 全排列II

题目: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 与上一题相比,这题多了一个无重复条件.那即在上一题的基础上加上去重处理即可. 去重办法: 首先,为了判别存在重复的数字,我们可以让重复的数字紧靠在一起,这样就可以用 if(nums[i] == nums[i-1]) 这样的方法来判重.那怎么让重复的数字紧靠在一起呢? 使用sort从小到大排序. 然后,使用上述的判重语句,并

[LeetCode] 267. Palindrome Permutation II 回文全排列 II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

LeetCode: Permutations II 题解

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1].题解:依旧使用的是DFS的思想. 首先需要遍历输入数组,获取一共有多少种不同的数字,每个数字有多少个. 最简单的方法,