当前位置:网站首页>剑指 Offer II 079. 所有子集
剑指 Offer II 079. 所有子集
2022-06-11 01:46:00 【彼淇梁】
剑指 Offer II 079. 所有子集【中等题】
思路:
【迭代】【二进制枚举】
与主站78题相同 78. 子集
代码:
class Solution {
public List<List<Integer>> subsets(int[] nums) {
//存储所有子集
List<List<Integer>> ans = new ArrayList<>();
//存储当前mask表示的子集
List<Integer> list = new ArrayList<>();
int n = nums.length;
//定义一个int类型变量mask,通过其二进制形式 表示 nums 中元素的取舍情况,进而表示 当前 mask 对应的子集
for (int mask = 0; mask < (1 << n); mask++) {
//首先对list进行清空,提高变量的复用性
list.clear();
//遍历nums数组,如果当前mask从右往左数第i位值为1,说明在nums数组中下标为i的元素被选中,将其添加到list集合中
for (int i = 0; i < n; i++) {
if ((mask & (1 << i)) != 0){
list.add(nums[i]);
}
}
//将当前mask表示的子集添加到ans中
ans.add(new ArrayList<>(list));
}
//所有可能的mask均被遍历完之后,得到的ans即为nums数组的所有子集
return ans;
}
}
边栏推荐
猜你喜欢

Colab报错:ImportError: cannot import name ‘_check_savefig_extra_args‘ from ‘matplotlib.backend_bases‘

可扩/减容线程池C语言原理讲解及代码实现

Metal organic framework MOF Al (Diba), MOF Zr (Diba), MOF Fe (Diba) loaded with curcumin / carboxybenzylpenicillin /mtx methotrexate / paclitaxel ptx/ DOX / cisplatin cddp/cpt camptothecin and other d

Epoll principle and Application & ET mode and lt mode
![[C language] storage of data in memory -1 plastic](/img/4a/24c1bb4743bd4ae965ed88f333f2fe.jpg)
[C language] storage of data in memory -1 plastic

Colab reported an error: importerror: cannot import name '_ check_ savefig_ extra_ args‘ from ‘matplotlib. backend_ bases‘

What do you know about the set class? Soul questions from Volume I

app 测试 常用 adb 命令集合

clang-format 最全格式说明

企业展厅设计能为企业带来什么?
随机推荐
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
Binary tree sequence traversal
SD3.0笔记
English subtitle video translated into Chinese subtitles
MySQL备份与恢复
Kotlin apply method
年金保險理財產品可以複利嗎?利率是多少?
Knowledge competition of safety production month -- how much do you know about new safety law
Find - (block find)
Shader of double sided material
SQL | return customer name, relevant order number and total price of each order
2022 simulated 100 questions and answers for crane driver (limited to bridge crane) examination
Core principle and code explanation of epoll reactor model
Xampp is used under M1 chip, and the installation extension error
UI interaction
Unity serial port communication
技术分享| 快对讲,全球对讲
Epoll principle and Application & ET mode and lt mode
1031. maximum sum of two non overlapping subarrays
叶酸配体的金属有机骨架材料MOFs负载5-氟尿嘧啶,西达本胺,紫杉醇,阿霉素,柔红霉素,布洛芬,喜树碱,姜黄素,藤黄酸等小分子药物