[LeetCode]题解(python):090 Subsets II
题目来源
https://leetcode.com/problems/subsets-ii/
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
题意分析
Input:
:type nums: List[int]
Output:
:rtype: List[List[int]]
Conditions:返回一个list的所有子集,注意list中的元素可以是重复的,但是要求返回的子集不重复,子集中的元素顺序为非降序。
题目思路
用dfs,同78题,只是在增加一个子集的时候,判断是否已经存在这个子集再增加。
附注:在扩展子集的时候,可以采用不断增加一个元素,并且先对list进行排序,每次增加时只遍历当前位置之后的元素,这样就可以避免重复。
AC代码(Python)
class Solution(object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def dfs(depth, start, valueList):
if valueList not in res:
res.append(valueList)
if depth == len(nums):
return
for i in range(start, len(nums)):
dfs(depth + 1, i + 1, valueList+[nums[i]]) nums.sort()
res = []
dfs(0, 0, [])
return res
[LeetCode]题解(python):090 Subsets II的更多相关文章
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 090 Subsets II 子集 II
给定一个可能包含重复整数的列表,返回所有可能的子集(幂集).注意事项:解决方案集不能包含重复的子集.例如,如果 nums = [1,2,2],答案为:[ [2], [1], [1,2,2], ...
- LeetCode(90) Subsets II
题目 Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- LeetCode 题解 | 面试题57 - II. 和为s的连续正数序列
题目描述 面试题57 - II. 和为s的连续正数序列 难度简单37收藏分享切换为英文关注反馈 输入一个正整数 target ,输出所有和为 target 的连续正整数序列(至少含有两个数). 序列内 ...
- LeetCode题解之Unique Paths II
1.题目描述 2.问题描述 使用动态规划算法,加上条件检测即可 3.代码 int uniquePathsWithObstacles(vector<vector<int>>&am ...
- Leetcode题解之Valid Palindrome II
1.题目描述 2.问题分析 使用两个下标,检测下标对应的字符是否相等,若不相等,则考察子串. 3.代码 bool validPalindrome(string s) { , j = s.size()- ...
- LeetCode题解之Contains Duplicate II
1.题目描述 2.题目分析 使用哈希表 和分情况讨论的方法 3.代码 bool containsNearbyDuplicate(vector<int>& nums, int k) ...
- [LeetCode 题解]: Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode题解]142. 环形链表 II | 快慢指针
解题思路 本题是在141. 环形链表基础上的拓展,如果存在环,要找出环的入口. 如何判断是否存在环,我们知道通过快慢指针,如果相遇就表示有环.那么如何找到入口呢? 如下图所示的链表: 当 fast 与 ...
随机推荐
- 走楼梯[XDU1031]
Problem 1031 - 走楼梯 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 724 Accep ...
- ssh org.springframework.beans.TypeMismatchException
这个问题我搜了一上午都木有解决 后来找到一个英文网站 It seems that some other bean (one that is not shown) has a property of t ...
- 【总结】String in Java
摘自:爪哇人的博客:http://hxraid.iteye.com/blog/522167/ J2SE - 语言基础与API JavaJVM虚拟机多线程数据结构 作者:每次上网冲杯Java时,都能看 ...
- Codeforces Round #213 (Div. 2) A. Good Number
#include <iostream> #include <vector> using namespace std; int main(){ ; cin >> n ...
- WebRTC手记之本地音频采集
转载请注明出处:http://www.cnblogs.com/fangkm/p/4374668.html 上一篇博文介绍了本地视频采集,这一篇就介绍下音频采集流程,也是先介绍WebRTC原生的音频采集 ...
- ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机
ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机 使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linu ...
- 【转载】Erlang 中 link 和 monitor 的区别
Link and Monitor differences 原文地址 Introduction link/1 and monitor/2 are 2 different ways of notifyin ...
- opengl中层次建模的实现
1.显示列表的创建 例如: glNewList(listID,listMode); glutSolidCube(2.0); ...... glEndlist(); 可以创建一个listID显示列表,l ...
- thinkphp模板中foreach循环没数据的错误解决
从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...
- 将url的查询参数解析成字典对象
1, 这个题目不约而同的出现在了多家公司的面试题中,当然也是因为太过于典型,解决方案无非就是拆字符或者用正则匹配来解决,我个人强烈建议用正则匹配,因为url允许用户随意输入,如果用拆字符的方式,有任何 ...