LeetCode78求集合的子集，集合是不重复的。

Given a set of distinct integers, nums, return all possible subsets (the power set).

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


package leetcode;

import java.util.ArrayList;
import java.util.List;

public class LC78SubSetsII {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();

int len = nums.length;
if(len == 0){
return ret;
}
List<Integer> sub = new ArrayList<Integer>();
subget(nums,0,ret,sub);

return ret;

}

public static void subget(int[] nums,int index,List<List<Integer>> ret,List<Integer> sub){
if(index == nums.length){
return;
}
subget(nums,index+1,ret,sub);//不要这个数
List<Integer> list = new ArrayList<Integer>(sub);
subget(nums,index+1,ret,list);

}

public static void main(String[] args) {
int[] nums={1,2,3};
List<List<Integer>> list = subsets(nums);
for(int i =0;i<list.size();i++){
for(int j =0;j<list.get(i).size();j++){
System.out.print(list.get(i).get(j));
}

System.out.println();

}

System.out.println(list.size());

}

}

package leetcode;

import java.util.ArrayList;
import java.util.List;

public class LC78SubSets {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();

int len = nums.length;
for(int i=0;i<len;i++){
int n = ret.size();
for(int j=0;j<n;j++){
List<Integer> temp = ret.get(j);
List<Integer> list = new ArrayList<Integer>();
for(int k=0;k< temp.size();k++){
}

}
List<Integer> list = new ArrayList<Integer>();

}
List<Integer> list = new ArrayList<Integer>();

return ret;

}

public static void main(String[] args) {
int[] nums={1,2,3};
List<List<Integer>> list = subsets(nums);
for(int i =0;i<list.size();i++){
for(int j =0;j<list.get(i).size();j++){
System.out.print(list.get(i).get(j));
}

System.out.println();

}

System.out.println(list.size());

}

}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120