LeetCode题解:Subsets

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

题意:给定元素,返回所有可能的子集

解决思路:类似于求组合那题的思路,先添加一个数,然后插入第二个数的时候不断复制

代码:

public class Solution {
    public List> subsets(int[] nums) {
        List> result = new ArrayList>();

        int subsetNum = (int)Math.pow(2, nums.length);
        Arrays.sort(nums);

        for(int i = 0; i < subsetNum; i++){
            result.add(new ArrayList());
        }

        for(int i = 0; i < nums.length; i++){
            for(int j = 1; j < subsetNum; j++){
                List temp = result.get(j);

                if(((j >> i) & 1) == 1){
                    temp.add(nums[i]);
                }
            }
        }

        return result;
    }
}

 

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…

【LeetCode OJ 078】Subsets浅析

题目: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,
    Ifnums=[1,2,3], a solution is:

     

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

    解题思路:搜了一下网上的解题思路,有一个感觉很巧妙。大致的思路是这样的,一个数组的子集个数是这个数组的长度的n次方(2^n),例如a[2]={1,2},子集个数就是2^2=4,二进制表示为00,01,10,11,分别表示空集、{1}、{2}、{12},具体的实现代码如下:

     

     

    public class Solution
    {
     public List> subsetsWithDup(int[] nums) 
     {
      List> result=new ArrayList>();
      //先对数组进行排序
      Arrays.sort(nums);
      int n=nums.length;
      //1< list=new ArrayList();
       for(int j=0;j  
    

OJ 078】Subsets浅析 题目:Given a set
of distinct integers,nums, return all possible subsets. Note: Elements
in a subset must be in non-descending order. The solu…

LeetCode78:Subsets

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

这道题可以使用两种方法求解,一是使用位操作,另外是使用深度优先搜索和回溯,但是我只想出了位操作,深度优先的方法是看了Discuss后想出来的。

解法一:位操作

对于数组[1,2,3],可以用一个下标0和1表示是否选择该数字,0表示未选择,1表示选中,那么每一组3个0和1的组合表示一种选择,3位共有8种选择,分别是:
000 对应[]
001 对应[3]
010 对应[2]
011 对应[2,3]
100 …
101
110
111
那么上面为1的位表示数组中该位被选中。
那么只需要遍历0到1<<
length中的数,判断每一个数中有那几位为1,为1的那几位即会构成一个子集中的一个元素。

runtime:8ms

class Solution {
public:
    vector> subsets(vector& nums) {
        int length=nums.size();
        sort(nums.begin(),nums.end());
        vector > result;
        for(int i=0;i<1< tmp;
            //计算i中有那几位为1
            for(int j=0;j

解法二:回溯法

还可以使用深度优先搜索来遍历数组,采用回溯法来剔除元素。使用一个变量来记录路径,每遍历到一个元素即表示找到一条路径,将其加入子集中。 对于数组[1,2,3] 从1开始递归查询2,3,对于2,继续向下搜索,搜索完后将2删除。 runtime:8ms

class Solution {
public:    
    //使用深度优先的回溯法
     vector> subsets(vector& nums) {
         vector> result;
         vector path;
         sort(nums.begin(),nums.end());
         result.push_back(path);
         dfs(nums,0,path,result);
         return result;
     }
     void dfs(vector& nums,int pos,vector & path,vector> & result)
     {
            if(pos==nums.size())
                return;

            for(int i=pos;i

 

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
contai…

相关文章