leetcode || 90、Subsets II

problem:

Given a collection of integers that might contain duplicates, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

For example,

If S = [1,2,2],
a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

Hide Tags

Array Backtracking

题意:找出一个带重复元素的数组的所有子序列(包含空集),子序列不能重复。

thinking:

(1)我的思路是,先求长度为K的子序列,K从0到N。前面一道题 subset 也是采用这种思路,但是没有重复元素。

(2)求长度为K的子序列:DFS深搜,也就是回溯法,开一个K大小的数组记录遍历的元素。

(3)重点在于如何去重。网上看到一个很好的思路:开一个数组记录重复元素的使用情况,当前元素与前一个元素相同时,

如果前一个元素没有使用,则该元素也不能使用,前一个元素使用过后,该元素才能使用。相当于规定了重复元素使用的顺序,

就避免了子序列的重复。

(4)我还尝试使用STL unique_copy()函数,但是该函数只是对相邻重复元素的去重,如果相同子序列不挨着,也不好排序,就没法去重了。

如果真要这么做,考虑使用string存储子序列,可以使用unique_copy()函数去重。

code:

class Solution {
private:
    vector<vector<int> > ret;
    vector<int> tmp;
    bool canuse[100];
public:
    vector<vector<int> > subsetsWithDup(vector<int> &S) {
        ret.clear();
            unsigned int n=S.size();
            if(n==0)
                return ret;
            sort(S.begin(),S.end());
            tmp.clear();
            ret.push_back(tmp);
            memset(canuse,true,sizeof(bool)*100);
            for(int k=1;k<=n;k++)
            {
                tmp.resize(k);
                dfs(0,n,S,k,0);
            }
            return ret;
            }
        protected:
            void dfs(int dep, int n, vector<int> &S,int k,int start)
            {
               if(dep==k)
               {
                   ret.push_back(tmp);
                   return;
               }
               for(int i=start;i<n;i++)
               {
                   if(i==0)
                   {
                    canuse[i]=false;
                    tmp[dep]=S[i];
                    dfs(dep+1,n,S,k,i+1);
                    canuse[i]=true;
                   }
                   else
                   {
                       if(S[i-1]==S[i] && canuse[i-1])
                           continue;
                       canuse[i]=false;
                       tmp[dep]=S[i];
                       dfs(dep+1,n,S,k,i+1);
                       canuse[i]=true;
                   }
               }
            }
};
时间: 2024-10-27 13:37:15

leetcode || 90、Subsets II的相关文章

LeetCode 90: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. For example, If nums = [1,2,2], a soluti

leetcode || 78、Subsets

problem: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [

leetcode || 47、 Permutations II

problem: 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]. Hide Tags Backtracking 题意:给定一个带重复数字的序列,输出其所有的排列

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2]

【LeetCode】90.Subsets II

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. For example,If nums = [1,2,2]

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2

[leetcode]Subsets II @ Python

原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicat

LeetCode: Subsets II [091]

[题目] Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solutio

[LeetCode] Subsets II [32]

题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,2], a solution