leetcode 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.

For example,

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

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

那么有几种情况呢,如果给你的数字长度为n的话,那么首先是空数组1;选一个呢,为n个;如此下去,一个的个数为2^n个子数组。

这里给出一个java代码:

import java.util.*;

public class Solution {
    public List<List<Integer>> subsets(int[] S) {
        int totalLength  = 1<<S.length; //总共的子数组的个数
        List<List<Integer>>  l = new ArrayList<List<Integer>>();
        Arrays.sort(S);//数组进行排序

        for (int i=0;i<totalLength;i++){
             ArrayList<Integer> tmp = new ArrayList<Integer>();
            for(int j=0;j<S.length;j++){
                if(((i >> j) & 1 )!= 0){ //将s[n]分别用二进制的n-1位代替
                   tmp.add(S[j]);
                }
               }
            l.add(tmp);
        }
    return l;
	}
	//测试主函数
    public static void main(String[] args) {
		int[] array = {1,2,3};
		Solution  s = new Solution();
		System.out.println(s.subsets(array));
	}
}

可能一下在看不懂上面的代码,特别是下面这段代码的意思。

 if(((i >> j) & 1 )!= 0)

看一个图,如下

上图中以S=[1 2 3 4]为例子,可以看到一个规律,就是在前面所有的subset的最后再添加一个位数,就成了新生成的subset。

或者,我们简单的一理解为,s[0]代码二级制数字最后一位为1,s[1]代码二进制数字倒数第二位为1,如此下去。

如上图中,倒数第二个subset为[2 3 4],那么这代表着其用二级制表示为1110 = 14 这好是其外围数组的下标,这样不能理解上面列出的那个段代码了(或者如下)

 if(((i >> j) & 1 )!= 0)

由上面图中的原理,这样就比较好的理解下面的python代码了:

def subset1(S):
    A=[[]]
    S.sort()

    for n in S:
        for i in range(len(A)):
            ss = A[i][:]
            ss.append(n)
            A.append(ss)
    return A

下面的python在原理上再效率上会有所提高:

def subset2(S):
    res = [[]]
    S.sort()

    for n in S:
        res = res + [x + [n] for x in res]

    return res
时间: 2024-10-12 08:24:37

leetcode Subsets的相关文章

[LeetCode] Subsets [31]

题目 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], [1,2,3]

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 (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

[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 求数组的所有子集

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], [1,2,3], [

Leetcode:Subsets 子集生成

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. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1

LeetCode: Subsets [078]

[题目] 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], [1,2,

Leetcode: Subsets &amp; SubsetsII

Subsets Description: 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],

[leetcode]Subsets @ Python

原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @param S, a list of integer # @return a list of lists of integer def subsets(self, S): def dfs(depth, start, valuelist): res.append(valuelist) if depth