题目描述
我对递归和回溯的理解还是有问题,这是一道很简单的题目这边先贴一个博客参考一下
参考博客
题解
import java.util.*;
public class Leetcode_78 {
public List<List<Integer>> subsets(int[] nums) {
List<Integer> templist = new ArrayList<>();
List<List<Integer>> ret = new ArrayList<>();
Arrays.sort(nums);
findSubset(nums,templist,ret,0);
return ret;
}
private void findSubset(int[] nums, List<Integer> templist, List<List<Integer>>ret, int start)
{
ret.add(new ArrayList<>(templist));
for(int i = start;i<nums.length;i++)
{
templist.add(nums[i]);
findSubset(nums,templist,ret,i+1);
templist.remove(templist.size()-1);
}
}
public static void main(String[] args)
{
int[] nums = {1,2,3,4,5};
Leetcode_78 a = new Leetcode_78();
List<List<Integer>>ret = a.subsets(nums);
System.out.println(ret.toString());
}
}