iteration的做法:
public class Solution {
public List> subsets(int[] S) {
List>results = new ArrayList>();
if(S.length == 0) {
return results;
}
Arrays.sort(S);
results.add(new ArrayList());
for (int i= 0; i < S.length; i++) {
List> newResults = newArrayList>();
for (List preResult :results) {
List newResult = new ArrayList(preResult);
newResult.add(S[i]);
newResults.add(newResult);
}
results.addAll(newResults);
}
returnresults;
}
}
recursion:
// recursion
List> helper(int S[],int start) {
List> result = new ArrayList>();
if (start == S.length) {
List list= new ArrayList();
result.add(list);
returnresult;
}
List> preResult = helper(S, start+1);
result.addAll(preResult);
for (List list : preResult) {
ListnewList = new ArrayList(list);
newList.add(0, S[start]);
result.add(newList);
}
return result;
}
014.8.3:
public List>subsets(int[] S) {
List> results = new ArrayList>();
if (S.length == 0) {
returnresults;
}
Arrays.sort(S);
List> binaryResults =generateBinarySubsets(S.length);
for (List binaryList : binaryResults) {
ListrealList = new ArrayList();
for (int i= 0; i < binaryList.size(); i++) {
if(binaryList.get(i).intValue() == 1) {
realList.add(S[i]);
}
}
results.add(realList);
}
return results;
}
private List>generateBinarySubsets(int n) {
int num = (int) Math.pow(2, n) -1 ; // stupid,used 2^n here...
List> preLists = new ArrayList>();
List init = new ArrayList();
preLists.add(init);
while (num > 0) {
List>newLists = new ArrayList>();
for (Listlist : preLists) {
List list0 = newArrayList(list);
List list1 = newArrayList(list);
list0.add(0, 0);
list1.add(0, 1);
newLists.add(list0);
newLists.add(list1);
}
num = num>> 1;
preLists.clear();
preLists.addAll(newLists);
}
return preLists;
}
Given a collection of integers that might containduplicates, S, return all possible subsets.
Note:
- Elements in a subset must be innon-descending order.
- The solution set must notcontain duplicate subsets.
For example,
If S = [1,2,2]
,a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
2014.8.4 update:
如果该数字等于前一个数字,那么只对于前一个数字已经append的list再次append
public class Solution {
public List>subsetsWithDup(int[] num) {
Arrays.sort(num);
List> result = new ArrayList>();
helper(num, 0, result);
return result;
}
List> helper(int[]num, int start, List> result) {
List> newCombinations = newLinkedList>();
if (start == num.length) {
List list = new LinkedList();
newCombinations.add(list);
result.addAll(newCombinations);
returnresult;
}
List> preResult =helper(num, start+1, result);
// no repeat,append currentnumber to all previous combinations
if (start < num.length-1&& num[start] != num[start+1]) {
for (List list : result) {
ListnewList = new LinkedList(list);
newList.add(0, num[start]);
newCombinations.add(newList);
}
} else {
// with repeat, only append current number tocombinations contains last same number
for (List list : preResult) {
ListnewList = new LinkedList(list);
newList.add(0,num[start]);
newCombinations.add(newList);
}
}
result.addAll(newCombinations);
return newCombinations;
}
}
2013: wrong answer
public class Solution {
public ArrayList>subsetsWithDup(int[] num) {
// Note: The Solution object is instantiatedonly once and is reused by each test case.
Arrays.sort(num);
HashSet> result = helper(num,num.length-1);
ArrayList> finalResult = newArrayList>();
finalResult.addAll(result);
return finalResult;
}
HashSet> helper(int[]num, int index) {
HashSet> result = new HashSet>();
if (index < 0) {
result.add(new ArrayList());
returnresult;
}
result = helper(num, index-1);
ArrayList> newSets = newArrayList>();
for (ArrayList subset : result) {
ArrayListnewSet = new ArrayList(subset);
newSet.add(num[index]);
newSets.add(newSet);
}
result.addAll(newSets);
return result;
}
}