Given a set of distinct integers, nums, return all possible subsets.
Analysis, just to use the former result and then add some elements to the result set.
在CC150 中这道题被称为Dynamic programming, I think so , because it uses the result from the last step and add a new element to the last result to get the new result.
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int length = nums.length;
if(length <= 0)
return result;
Arrays.sort(nums);
List<Integer> tem = new ArrayList<Integer>();
result.add(tem);
for(int i = 0; i < length; i++){
int size = result.size();
for(int j = 0; j < size; j++){
List<Integer> newone = new ArrayList(result.get(j));
newone.add(nums[i]);
result.add(newone);
}
}
return result;
}
}