您的位置:首页 > 其它

Leetcode 78. Subsets

2016-01-23 14:37 399 查看
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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: