(LeetCode 78)SubSets

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.

题目要求 :求整数数组的所有子集

注意:

1、子集元素按非降序排列

2、不包含重复的子集

解题思路:

求解这类诸如子集的题目,都可以采用回溯法。(剪枝+递归)

代码如下:

class Solution {
private:
    vector<vector<int> > ans;
public:
    void collectSubSet(vector<int> &S,vector<int> x,int len,int idx){
        if(idx==len){
            vector<int> subset;
            for(int i=0;i<len;i++){
                if(x[i]!=0)
                    subset.push_back(S[i]);
            }
            sort(subset.begin(),subset.end(),less<int>());
            ans.push_back(subset);
            return;
        }
        x[idx]=0;
        collectSubSet(S,x,len,idx+1);
        x[idx]=1;
        collectSubSet(S,x,len,idx+1);
    }

    vector<vector<int> > subsets(vector<int> &S) {
        int len=S.size();
        vector<int> x(len);
 //       sort(S.begin(),S.end(),greater<int>());
        collectSubSet(S,x,len,0);
//        sort(ans.begin(),ans.end(),cmp());
        return ans;
    }
};
时间: 2024-12-26 09:48:30

(LeetCode 78)SubSets的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

ZigZag问题的一种新思路(Leetcode #6)

原始问题(leetcode ZigZag Conversion) 如果固定一个行数(这里是3),字符串 "PAYPALISHIRING" 用"之"字形的方式可以写成: P A H N A P L S I I G Y I R 这个结果按照一行一行访问的话是: "PAHNAPLSIIGYIR".给定一个字符串以及一个表示行数的整数,实现一个函数,返回按行序拼接而成的新串. string convert(string text, int nRows);

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

(LeetCode 72)Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

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题解)Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

(leetcode题解)Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 这是一道经典的题目,给定一个数组求和最大的子数组.算法导论对这道

(LeetCode 41)First Missing Positive

Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 题目: 给一无序数组,找到数组中从1开始第一个不出现的正整数. 要求O(n)的时间复杂度和常数空间复杂度

(LeetCode 49)Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题目: 给一组字符串,返回所有满足Anagrams(回文构词法)的字符串: Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”. 回文构词法有一个特点:单词里的字母的种类和数目没