51 回溯算法求解子集

发布时间:2023年12月21日

问题描述:给定一组不含重复元素的整数数组nums,返回该数组所有可能的子集。说明:解集不允许存在重复的子集;

回溯算法求解:最多进行nums.length次深度的回溯,每一次向下走都都记录一种结果,随着index一直向下走,以index为起点不可选择前面的元素。

public void tranceBack(int []nums,LinkedList<Integer>templist,LinkedList<LinkedList<Integer>>res,int index)
{
res.add(new linkedList<Integer>(templist));
for(int i=index;i<nums.length;i++)
{
templist.add(nums[i]);
tranceBack(nums,templist,res,i+1);
templist.remove(templist.size()-1);
}
}
public List<List<Integer>> TranceBack(int []nums)
{
List<List<Integer>>res=new LinkedList<LinkedList<Integer>>();
tranceBack(nums,new LinkedList<Integer>(),res,0);
???????return res;
}

文章来源:https://blog.csdn.net/qq_52299902/article/details/135132214
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。