leetcode-subset 发表于 2016-10-01 | 分类于 算法 , leetcode | 题目大意 https://leetcode.com/problems/subsets/ 输出数组的全部子集。 题目分析 输出一个数组的全部子集,这里采用暴力的深度优先搜索。 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748/* Given a set of distinct integers, nums, return all possible subsets. Note: 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*/public class Solution { private List<List<Integer>> res = new ArrayList<List<Integer>>(); private Boolean[] v; private void search(int i, int[] nums) { int len = nums.length; if(i >= len) { List<Integer> ans = new ArrayList<Integer>(); for(int ii = 0; ii < len; ii++) { if(v[ii] == true) { ans.add(nums[ii]); } } res.add(ans); return; } v[i] = true; search(i + 1, nums); v[i] = false; search(i + 1, nums); } public List<List<Integer>> subsets(int[] nums) { int len = nums.length; v = new Boolean[len]; res.clear(); search(0, nums); return res; }}