awesome guy
def subsets(self, nums):
# initialize a base list with an empty list because the empty is a subset of the main list 'nums'
result = [[]]
# for each number in the nums list
for num in nums:
# for each item in the results list,
#take it and append num to it, and then extend that into the result list.
result += [r + [num] for r in result]
return result
通用structure一些总结 :
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
backtrack(res, new ArrayList<>(), nums, 0);
return res;
}
private void backtrack(List<List<Integer>> res, List<Integer> tmp, int[] nums, int start){
res.add(new ArrayList<>(tmp));
for( int i = start ; i < nums.length ; i ++ ){
tmp.add(nums[i]);
backtrack(res, tmp, nums, i + 1);
tmp.remove(tmp.size() - 1);
}
}
}