您的位置:首页 > 其它

[LeetCode]Subsets

2016-04-27 10:45 411 查看
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],
[]
]


Subscribe to see which companies asked this question
题解:使用递归
code:
public class Solution {
public List<List<Integer>> subsets(int[] nums) {

Arrays.sort(nums);
List<List<Integer>> ans = new ArrayList<List<Integer>>();
List<Integer> res = new ArrayList<Integer>();
sovle(0, nums.length, nums, res, ans);
return ans;
}
private void sovle(int cur ,int length, int[] nums, List<Integer> res, List<List<Integer>> ans)
{
ans.add(new ArrayList<Integer>(res));
for(int i = cur; i<length; i++)
{
res.add(nums[i]);
sovle(i+1 ,length, nums, res, ans);
res.remove(res.size() - 1);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: