题目描述:

方法一:回溯

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. <Git>git学习

    1.安装 分布式版本控制:工作电脑保存完整的代码,中央服务器挂了也可以使用 集中式版本控制:中央服务器挂了就凉凉 sudo apt-get install git git安装 检测安装成功 git 2 ...

  2. 令人清爽的异步函数async、await

    1.什么是async.await? async用于声明一个函数是异步的.而await从字面意思上是"等待"的意思,就是用于等待异步完成.并且await只能在async函数中使用; ...

  3. ES6数组Api扩充

    1.  Array.of( );     ----将一组数据转换成一个数组: const num=201314; const a=Array.of(num); console.log(a); //数组 ...

  4. API Gateway和Route 53及CloudFront的连携使用

    API Gateway部署出来之后的url网址对于普通用户并不友好,所以肯定是需要一个正常的域名来作为url进行访问. 主要有以下几点, API Gateway可以自定义域名 自定义的域名要从Rout ...

  5. jq-demo-点击选择(英雄联盟)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 安装memcached报错:If it's already installed, specify its path using --with-libevent=/dir/

    一.安装memcached,执行./configure --prefix=/usr/local/memcached时候报错: 问题:If it's already installed, specify ...

  7. 线程池 一 ThreadPoolExecutor

    java.util.concurrent public class ThreadPoolExecutor extends AbstractExecutorService ThreadPoolExecu ...

  8. Dart编程数字Number

    Dart数字可以分为: int - 任意大小的整数. int 数据类型用于表示整数. double -64位(双精度)浮点数,由IEEE 754标准规定. 在 double 数据类型用于表示小数 in ...

  9. DataWorks2.0的“业务流程”与1.0的“工作流”的对比

    DatwWorks终于升级2.0了,心情万分激动之余,又有一丝担忧.因为,没法再创建新的旧版工作流了...新版抛弃了“工作流”这个概念,引入了“业务流程”和“解决方案”两个新的概念.于是,作为团队Le ...

  10. bzoj1013题解

    [解题思路] 初看以为是二次方程组,但这些方程有相同的右值r2,于是可以化为一次方程组,高斯消元即可.复杂度O(n3). 化简过程: 假设第i个方程和第j个方程联立,得:  ∑(a[i,k]-a[0, ...