leetcode No78. Subsets

Question:

Given a set of distinct integers, nums, return all possible subsets.

Note: 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],
  []
]

Algorithm:

也是用二叉树的思想,不过这次不需要判断长度,每更新一次数组,就赋值给二维数组(最终结果)。如果数组长度等于nums的长度就返回。

Accepted Code:

class Solution {
public:
    vector<vector<int>> res;
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<int> cur;
        BFS(nums,cur,0);

        return res;
    }
    void BFS(vector<int>& nums,vector<int>& cur,int k)
    {
        if(cur.size()==nums.size())
        {
            res.push_back(cur);
            return;
        }
        else
        {
            res.push_back(cur);
            for(int i=k;i<nums.size();i++)
            {
                vector<int> temp=cur;
                temp.push_back(nums[i]);
                BFS(nums,temp,i+1);
            }
        }
    }
};
时间: 2024-10-05 19:25:17

leetcode No78. Subsets的相关文章

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[

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]

Java for LeetCode 090 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】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], a solutio

[leetcode]90. Subsets II数组子集(有重)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

LeetCode 90. Subsets II (子集合之二)

Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a solution is: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题目标签:Arr

深度优先搜索算法(DFS)以及leetCode的subsets II

深度优先搜索算法(depth first search),是一个典型的图论算法.所遵循的搜索策略是尽可能“深”地去搜索一个图. 算法思想是: 对于新发现的顶点v,如果它有以点v为起点的未探测的边,则沿此边继续探测下去.当顶点v的所有边都已被探寻结束,则回溯到到达点v的先辈节点.以相同方法一直回溯到源节点为止.如果图中还有未被发现的顶点,则选择其中一个作为源顶点,重复以上的过程.最后的结果是一些不相交的深度优先树. leetCode中的应用: Given a collection of integ

【leetcode】Subsets (Medium) ☆

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  3  4 先把输入从小到大排序 长度 0 : [] 长度 1 : 把输入数据从第一