SubsetsTotal Accepted:49746Total Submissions:176257My Submissions
Subsets
Total Accepted: 49746 Total Submissions: 176257My Submissions
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],
[]
]
package hashmap;
import java.util.ArrayList;
import java.util.Arrays;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] s={1,2,3};
ArrayList<ArrayList<Integer>> result=subsets(s);
for ( int i = 0; i < result.size(); i++){
System.out.println(result.get(i));
}
}
public static void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> l, int[] s, int pos){
System.out.println("total pos:"+pos);
if(pos == s.length){
res.add(new ArrayList(l));
System.out.println("----------------------");
for ( int i = 0; i < res.size(); i++)
System.out.println("res"+res.get(i));
System.out.println("----------------------");
return ;
}
dfs(res, new ArrayList(l), s, pos+1);
l.add(s[pos]);
System.out.println("addelem:"+pos+":"+s[pos]);
System.out.println("dfs infor:"+pos);
dfs(res,l, s, pos+1);
System.out.println("l----------------------");
for ( int k = 0; k < l.size(); k++){
System.out.println(l.get(k));
}
System.out.println("l----------------------"); System.out.println("l----------------------");
}
public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> l = new ArrayList<Integer>();
Arrays.sort(S);
dfs(res, l, S, 0);
return res;
}
}
------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园