47. 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],
  [2,1,1]
]

与上一题不同,就是在19行加个判断即可。
 1 class Solution(object):
 2     def __init__(self):
 3         self.res = []
 4
 5     def permuteUnique(self, nums):
 6         """
 7         :type nums: List[int]
 8         :rtype: List[List[int]]
 9         """
10         self.help(nums, 0, len(nums))
11
12         return self.res
13
14     def help(self, a, lo, hi):
15         if(lo == hi):
16             self.res.append(a[0:hi])
17         for i in range(lo, hi):
18             #判断 i 是否已经在当过头元素了
19             if a[i] not in a[lo:i]:
20                 self.swap(a, i, lo)
21                 self.help(a, lo + 1, hi)
22                 self.swap(a, i, lo)
23     def swap(self, a, i, j):
24         temp = a[i]
25         a[i] = a[j]
26         a[j] = temp

原文地址:https://www.cnblogs.com/zle1992/p/8448774.html

时间: 2024-10-09 16:18:15

47. Permutations II (全排列有重复的元素)的相关文章

[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] O

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]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

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随着计

47. 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]. 链接: http://leetcode.com/problems/permutations-ii/ 4/15

[leedcode 47] 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]. public class Solution { List<Integer> seq; List<List&l

(待解决,效率低下)47. Permutations II C++回溯法

思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果 但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的vector中时,会出现memory limit exceeded错误(原因??) 上网查找后发现可以直接通过return vector<vector<int>> (mySet.begin(),mySet.end())得到结果,并且代码通过. class Solution { publi

LeetCode 【47. 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], [2,1,1] ] 思路1.这题与Permutations的区别在于他允许重复数字,最简单的就是利用1的结果,保存在一个set中,去重复

47. Permutations II java solutions

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], [2,1,1] ] 1 public class Solution { 2 List<List<Integer>> ans

47. Permutations II (java )

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. 解析: 编码: