题目描述:

方法一:回溯

class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
nums.sort()
if not nums:
return []
n = len(nums)
res = []
def backtrack(i,temp):
if temp not in res:
res.append(temp)
for j in range(i,n):
backtrack(j+1,temp+[nums[j]])
backtrack(0,[])
return res

另:优化

class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
res = []
n = len(nums)
nums.sort()
def helper(idx, tmp):
res.append(tmp)
for i in range(idx, n):
if i > idx and nums[i] == nums[i-1]:
continue
helper(i+1, tmp + [nums[i]])
helper(0, [])
return res

方法二:迭代

class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
if not nums: return []
nums.sort()
res = [[]]
cur = []
for i in range(len(nums)):
if i > 0 and nums[i - 1] == nums[i]:
cur = [tmp + [nums[i]] for tmp in cur]
else:
cur = [tmp + [nums[i]] for tmp in res]
res += cur
return res

leetcode-90-子集②的更多相关文章

  1. Java实现 LeetCode 90 子集 II(二)

    90. 子集 II 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [ ...

  2. [leetcode] 90. 子集 II.md

    90. 子集 II 78. 子集题的扩展,其中的元素可能会出现重复了 我们仍沿用78题的代码,稍作改动即可: 此时需要对nums先排个序,方便我们后面跳过选取相同的子集. 跳过选取相同的子集.当选取完 ...

  3. leetcode 90. 子集 II JAVA

    题目: 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2] ...

  4. Leetcode 90.子集

    子集 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2], ...

  5. Leetcode 90. 子集 II

    地址  https://leetcode-cn.com/problems/subsets-ii/ 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重 ...

  6. LeetCode 90. 子集 II(Subsets II)

    题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: [ [2], [1], [1,2,2 ...

  7. LeetCode -90. 子集 II C++ (回溯法)

    class Solution { public: vector<vector<int>> subsetsWithDup(vector<int>& nums) ...

  8. 每日一题-——LeetCode(78)子集

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  [1,2,3],  [1,3],  [2, ...

  9. [Leetcode 90]求含有重复数的子集 Subset II

    [题目] Given a collection of integers that might contain duplicates, nums, return all possible subsets ...

  10. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

随机推荐

  1. leetcode.字符串.5最长回文子串-Java

    1. 具体题目 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" ...

  2. vmstat - 报告虚拟内存的统计信息

    总览 vmstat [-n] [延时[次数]] vmstat [-V] 描述 vmstat 对系统的进程情况.内存使用情况.交换页和 I/O 块使用情况.中断以及 CPU 使用情况进行统计并报告相应的 ...

  3. Spring接收数据,传递数据

    Spring接收数据,传递数据 前提配置 POM   <dependency> <groupId>org.springframework</groupId> < ...

  4. notepad++ remove duplicate

    step1 to sort and remove space. Since Notepad++ Version 6 you can use this regex in the search and r ...

  5. python 将字符串转换成字典dict

    JSON到字典转化:>>>dictinfo = json.loads(json_str) 输出dict类型 字典到JSON转化:>>>jsoninfo = json ...

  6. 夯实JavaScript基础之prototype, __proto__, instanceof

    function New(f){ return function(){ var o = {'__proto__': f.prototype}; f.apply(o, arguments); retur ...

  7. *** stack smashing detected ***: ./server terminated

    该类错误是修改了返回指针,一般是由于 1. 数组越界赋值.(数组没有边界检查)int a[8]; a[8],a[9],a[-1]..都能正常编译,连接,运行时可能出错. 2.使用 strcpy等不安全 ...

  8. 建站租用RAKsmart服务器的优势

    RAKsmart算是近年来受国内用户关注度颇高的美国服务器提供商.位于美国西海岸加州地区的RAKsmart机房,拥有超过十年的机房管理经验,提供了快速.稳定的服务器租用服务.那么RAKsmart服务器 ...

  9. Redis探索之路(三):Redis的五种数据类型String和Hash

    一:String 存储二进制数据,可以图片,序列化对象 GET,SET SETNX(not exist)  setnx age 33 返回 0,1 SETEX设置有效期   SETEX COLOR 2 ...

  10. thinkphp PATH_INFO支持

    如果发生在本地测试正常,但是一旦部署到服务器环境后会发生只能访问首页的情况,很有可能是你的服务器或者空间不支持PATH_INFO所致. 系统内置提供了对PATH_INFO的兼容判断处理,但是不能确保在 ...