Lintcode18 Subsets II solution 题解

【题目描述】

Given a list of numbers that may has duplicate numbers, return all possible subsets

Notice:Each element in a subset must be in non-descending order.The ordering between two subsets is free.The solution set must not contain duplicate subsets.

给定一个可能具有重复数字的列表,返回其所有可能的子集

注意:子集中的每个元素都是非降序的;两个子集间的顺序是无关紧要的;解集中不能包含重复子集

【题目链接】

http://www.lintcode.com/en/problem/subsets-ii/

【题目解析】

经典的DFS问题,如果有跟过九章微博的同学 应该会相当熟悉这个套路,跟前一个题目SubSet的区别是,有了重复的问题。怎么解决呢?

很简单。在每一次选数字的时候,只选第一个重复的数字,不选后面的,这样就不会有重复的set出现了。这里肯定有同学问了,如果你只选第一个,那222这种组合怎么弄出来?答案是:用递归时就不要考虑太多,只要考虑当前的情况。

例子: 1 2 2 2 2 3 4

那么你得到2 2 2 的过程是三层递归,每一层 都只选当前index开始的第一个2,所以2 2 2 还是可以组出来的。而且不会组出重复的,因为每一层递归你没有考虑重复,这就可以了。

还是要记住递归的精髓:考虑本层递归就好,别想太多

【答案链接】

https://www.jiuzhang.com/solutions/subsets-ii/

时间: 2024-10-12 20:21:35

Lintcode18 Subsets II solution 题解的相关文章

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

Lintcode16 Permutations II solution 题解

[题目描述] Given a list of numbers with duplicate number in it. Find all unique permutations. 给出一个具有重复数字的列表,找出列表所有不同的排列. [题目链接] http://www.lintcode.com/en/problem/permutations-ii/ [题目解析] 跟 Permutations的解法一样,就是要考虑"去重".先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否

Lintcode70 Binary Tree Level Order Traversal II solution 题解

[题目描述] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) [题目链接] www.lintcode.com/en/problem/binary

Lintcode34 N-Queens II solution 题解

[题目描述] Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions. 根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局. [题目链接] http://www.lintcode.com/en/problem/n-queens-ii/ [题目解析] 这道题跟NQueens的解法完全一样(具

Lintcode54 String to Integer II solution 题解

[题目描述] Implement function atoi to convert a string to an integer.If no valid conversion could be performed, a zero value is returned.If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is re

Lintcode36 Reverse Linked List II solution 题解

[题目描述] Reverse a linked list from position m to n. Notice:Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. 翻转链表中第m个节点到第n个节点的部分 注意:m,n满足1 ≤ m ≤ n ≤ 链表长度 [题目链接] http://www.lintcode.com/en/problem/reverse-linked-list-ii/ [题目解析] 反转

Lintcode190 Next Permutation II solution 题解

[题目描述] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). 给定一个若干

Lintcode207 Interval Sum II solution 题解

[题目描述] Given an integer array in the construct method, implement two methods query(start, end) and modify(index, value): For query(start,end), return the sum from index start to index end in the given array. For modify(index,value), modify the number

Lintcode364 Trapping Rain Water II solution 题解

[题目描述] Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1x1, compute how much water it is able to trap after raining. 给定n x m非负整数,表示二维的海拔地图,每个单元的面积是1 x 1,计算下雨后能捕到多少水. [题目链接] www.lintcode.com/en/problem