Subset leetcode java

题目

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

题解:一个思路就是套用combination的方法,其实combination那道题就是在求不同n下的subset,这里其实是要求一个集合罢了。例如k=3,n=1,用combination那道题的方法求得集合是[[1], [2], [3]];    k=3, n=2, 用combination那道题的方法求得集合是[[1, 2], [1, 3], [2, 3]]    k=3, n=3, 用combination那道题的方法求得集合是[[1,2,3]]所以上述3个集合外加一个空集不就是[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]么?只需要在combination的外面加个循环即可。

代码如下:

1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         if(item.size()==len){
 3             res.add(new ArrayList<Integer>(item));
 4             return;
 5         }
 6         for(int i=start; i<S.length;i++){
 7             item.add(S[i]);
 8             dfs(S, i+1, len, item, res);
 9             item.remove(item.size()-1);
10         }
11 
12     }
13     
14     public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
15         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
16         ArrayList<Integer> item = new ArrayList<Integer>();
17         if(S.length==0||S==null)
18             return res;
19         
20         Arrays.sort(S);
21         for(int len = 1; len<= S.length; len++)
22             dfs(S,0,len,item,res);
23             
24         res.add(new ArrayList<Integer>());
25         
26         return res;
27     }

Reference:http://blog.csdn.net/worldwindjp/article/details/23300545

底下是另外一个很精炼的算法。

1  public static void dfs(int[] S, int start, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
 2         for(int i=start; i<S.length;i++){
 3             item.add(S[i]);
 4             res.add(new ArrayList<Integer>(item));
 5             dfs(S,i+1, item,res);
 6             item.remove(item.size()-1);
 7         }
 8 
 9     }
10     
11     public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
12         ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
13         ArrayList<Integer> item = new ArrayList<Integer>();
14         if(S.length==0||S==null)
15             return res;
16         
17         Arrays.sort(S);
18         dfs(S,0,item,res);
19         res.add(new ArrayList<Integer>());
20         
21         return res;
22     }

 Reference:http://blog.csdn.net/u011095253/article/details/9158397

Subset leetcode java

时间: 2024-10-18 18:28:45

Subset leetcode java的相关文章

Subset II leetcode java

题目: 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 solution

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: 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] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的

Triangle leetcode java

题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 1

[Leetcode][JAVA] Pascal&#39;s Triangle I, II

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] ] 已知行数生成帕斯卡三角.实际上只要有第i层,那么就能生成第i+1层.每次新生成的层加入最终集合中即可. 1 public List<List<Integer&g

Permutations II leetcode java

题目: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 题解: 这道题跟Permutaitons没啥大的区别,就是结果去重. 我之前也有写过去重的两个方法: 一

Word Ladder leetcode java

题目: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one letter can be changed at a time Each intermediate word must exist in the dictionary For example, Given: