[Leetcode]Subsets

原创 2015年11月20日 16:37:13

Subsets My Submissions Question
Total Accepted: 72388 Total Submissions: 247343 Difficulty: Medium
Given a set of distinct integers, 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,3], a solution is:

[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Subscribe to see which companies asked this question
我是按照我博客里那种方法做的,有些地方不太一样,用adapter模式(允许我装个逼)搞一下,结果对顺序还有要求。。。干脆每次插入都排个序,这样时间就上去了,12ms。如果不排序就要修改一下getSubstring()这个就行,可是我懒得修改了。当然用DFS做也没问题,就比较简单了。下面把两种方法都贴出来

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        int len = nums.size();
        string str;
        for(int i = 0;i != len;++i){
            str.push_back(nums[i] + '0');
        }

        vector<string> res = func(str);

        vector<vector<int> >    ans;
        ans.push_back({});
        for(int i = 0;i != res.size();++i){
            vector<int> tem;
            for(int j = 0;j != res[i].size();++j){
                tem.push_back(res[i][j] - '0');
            }
            sort(tem.begin(),tem.end());
            ans.push_back(tem);
        }
        sort(ans.begin(),ans.end());
        return ans;
    }

vector<string> func(string str){
    int len = str.size();
    vector<string>  res;
    int num = 1 << len;
    num = num - 1;
    //cout << num << endl;
    for(int i = 1;i <= num;++i){
        string tem = getSubstring(str,i);
        res.push_back(tem);
    }
    return res;
}
string getSubstring(string str,int num){
    string tem;
    //cout << num << endl;
    int len = str.size();
    int n = len - 1;
    while(num){
        if(num & 0x1){
            tem += str[n];
        }
        num = num >> 1;
        --n;
    }
    return tem;
}
};

DFS:

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    vector<vector<int>> res;
    vector<int> path;
    dfs(nums, 0, path, res);
    return res;
}

void dfs(vector<int>& nums, int index, vector<int>& path, vector<vector<int>>& res) {
    res.push_back(path);
    for (unsigned int i = index; i < nums.size(); i++) {
        path.push_back(nums[i]);
        dfs(nums, i+1, path, res);
        path.pop_back();
    }
}
};
版权声明:转载请注明出处

相关文章推荐

Leetcode : Subsets II

URL:https://leetcode.com/problems/subsets-ii/#/description题目大意:Given a collection of integers that m...

leetcode 90. Subsets II

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

【leetcode】90. Subsets II

leetcode, subsets ||, 3种解法

leetcode:暴力枚举法之Subsets II

leetcode:暴力枚举法之Subsets II 题目; Given a collection of integers that might contain duplicates, S, retur...

LeetCode OJ:Subsets II

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

leetcode-78 Subsets

分析:求集合的所有子集问题。题目要求子集中元素非递减序排列,因此我们先要对原来的集合进行排序。原集合中每一个元素在子集中有两种状态:要么存在、要么不存在。这样构造子集的过程中每个元素就有两种选择方法:...
  • sole_cc
  • sole_cc
  • 2015年03月28日 19:19
  • 379

[leetcode 78 & 90, Medium] Subsets I and II

Subsets I and II

Subsets II -- leetcode

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

leetcode 90 Subsets II

leetcode 90 Subsets II c++ Python

[LeetCode][Java] Subsets II

题目: Given a collection of integers that might contain duplicates, nums, return all possible sub...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[Leetcode]Subsets
举报原因:
原因补充:

(最多只允许输入30个字)