网站规划开发前景,关键词排名查询软件,网站建设硬件预算,公司搭建网站步骤1.问题描述 给你一个整数数组 nums #xff0c;数组中的元素 互不相同 。返回该数组所有可能的
子集#xff08;幂集#xff09;。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例1 输入#xff1a;nums [1,2,3]输出#xff1a;[[],[1],[2],[1,2],[3],[1…1.问题描述 给你一个整数数组 nums 数组中的元素 互不相同 。返回该数组所有可能的
子集幂集。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例1 输入nums [1,2,3]输出[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] 示例2 输入nums [0]输出[[],[0]] 提示
1 nums.length 10-10 nums[i] 10nums 中的所有元素 互不相同 难度等级 中等 题目链接 子集
2.解题思路 这道题要求所有可能的子集这道题和求全排列那道题的不同之处在于全排列那道题(1,2,3)和(2,1,3)是两个不同的排列而(1,2,3)和(2,1,3)是两个相同的子集取其中一个就可以了所以这道题起始比全排列那道题简单一些。 同样是使用递归回溯的方法来解决这道题不过这时候我们就不需要用一个标识数组来标识某个数是否已经被使用过我们可以直接通过索引来排除掉重复的子集。每一次递归都从当前索引的下一个数开始这样后面的所有子集就不可能出现当前索引及其之前的数这样就不会出现(1,2,3)和(2,1,3)同时出现的情况。 有了基本思路之后我们就可以来实现这个基本思路首先是定义一个存储最终答案的List集合接着我们要来分析一下递归回溯函数该如何实现。 //存储最终答案ListListInteger data new ArrayList(); 首先我们来确定递归的结束条件因为我们通过索引来实现去重索引当索引越界的时候递归就可以结束直接返回了因为索引越界也就代表了所有的数都已经去完了。 //当起始索引越界时返回if(startIndex nums.length){return;} 接着我们来确定一下递归方法需要传入的参数。我们需要传入存储最终答案的List集合我们还需要一个辅助List集合来帮我们存储当前子集的情况同时还要传入题目给的数组和递归方法的起始索引。 public void backtrack(ListListIntegerdata, ListInteger list,int[] nums,int startIndex){.......
} 从给出的两个示例可以看出没有元素也是一个子集所以我们一开始就可以将空集啥也没有存入到答案List中判断完递归的结束条件之后我们就可以正式进入递归逻辑了(起始前面的空集存入答案List就已经是递归逻辑的一部分了)。 //将当前子集存入答案List中data.add(new ArrayList(list));//当辅助list中的元素个数与nums中相等时返回if(startIndex nums.length){return;} 我们从起始索引的位置开始遍历题目给的数组将每一个数加入当前的子集中接着递归调用当前的方法求包含当前子集的其他子集这里传入的起始索引是(i1)也就是当前索引1这样就不会取到前面的数和当前刚刚存入子集的数了。求完包含当前子集的其他子集之后我们要将当前的数从子集中取出来进行回溯因为包含这个数在当前位置的子集我们已经求完了可以将它舍弃掉了。 //从起始索引开始遍历nums数组for(int i startIndex;i nums.length;i){//将数加入子集中list.add(nums[i]);//i1避免取到前面已经取过的数backtrack(data,list,nums,i1);//回溯list.remove(list.size()-1);} 递归函数结束之后我们就可以得到所有的子集了这时直接将结果返回即可。 //递归函数backtrack(data,new ArrayList(),nums,0);//返回结果return data;
3.代码展示
class Solution {public ListListInteger subsets(int[] nums) {//存储最终答案ListListInteger data new ArrayList();//递归函数backtrack(data,new ArrayList(),nums,0);//返回结果return data;}public void backtrack(ListListIntegerdata, ListInteger list,int[] nums,int startIndex){//将当前子集存入答案List中data.add(new ArrayList(list));// 当起始索引越界时返回if(startIndex nums.length){return;}//从起始索引开始遍历nums数组for(int i startIndex;i nums.length;i){//将数加入子集中list.add(nums[i]);//i1避免取到前面已经取过的数backtrack(data,list,nums,i1);//回溯list.remove(list.size()-1);}}
}
4.总结 这道题与第46题的求全排列大同小异都用到了递归回溯这道题是使用了起始索引来实现去重和标识是否已经使用而全排列那道题不需要去重通过一个标识数组来标识是否已经使用都是简单的for循环回溯即可解决了。这道题今天就啰嗦到这祝大家刷题愉快早日上岸 文章转载自: http://www.morning.mmosan.com.gov.cn.mmosan.com http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.zfqr.cn.gov.cn.zfqr.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.dhqg.cn.gov.cn.dhqg.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.kdbcx.cn.gov.cn.kdbcx.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.btmwd.cn.gov.cn.btmwd.cn http://www.morning.ctqbc.cn.gov.cn.ctqbc.cn http://www.morning.qdmdp.cn.gov.cn.qdmdp.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.rjnky.cn.gov.cn.rjnky.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.nlnmy.cn.gov.cn.nlnmy.cn http://www.morning.tsqpd.cn.gov.cn.tsqpd.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.crqpl.cn.gov.cn.crqpl.cn http://www.morning.zzgkk.cn.gov.cn.zzgkk.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.bmncq.cn.gov.cn.bmncq.cn http://www.morning.dpppx.cn.gov.cn.dpppx.cn http://www.morning.jjzbx.cn.gov.cn.jjzbx.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.ypqwm.cn.gov.cn.ypqwm.cn http://www.morning.jzccn.cn.gov.cn.jzccn.cn http://www.morning.zzfjh.cn.gov.cn.zzfjh.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.dsprl.cn.gov.cn.dsprl.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.fnfxp.cn.gov.cn.fnfxp.cn http://www.morning.pmrlt.cn.gov.cn.pmrlt.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.rtzd.cn.gov.cn.rtzd.cn http://www.morning.jxzfg.cn.gov.cn.jxzfg.cn http://www.morning.qpljg.cn.gov.cn.qpljg.cn http://www.morning.qwbht.cn.gov.cn.qwbht.cn http://www.morning.btgxf.cn.gov.cn.btgxf.cn http://www.morning.srtw.cn.gov.cn.srtw.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.xqbbc.cn.gov.cn.xqbbc.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.hlppp.cn.gov.cn.hlppp.cn http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.kkwbw.cn.gov.cn.kkwbw.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.mfcbk.cn.gov.cn.mfcbk.cn http://www.morning.kgnrh.cn.gov.cn.kgnrh.cn http://www.morning.kpxky.cn.gov.cn.kpxky.cn http://www.morning.hbkkc.cn.gov.cn.hbkkc.cn http://www.morning.bkqw.cn.gov.cn.bkqw.cn http://www.morning.bdtpd.cn.gov.cn.bdtpd.cn http://www.morning.kzslk.cn.gov.cn.kzslk.cn http://www.morning.jcrfm.cn.gov.cn.jcrfm.cn http://www.morning.ktpzb.cn.gov.cn.ktpzb.cn http://www.morning.c7625.cn.gov.cn.c7625.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn