78.子集

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

java 复制代码
class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();

        for ( int i = 0; i < (1<<nums.length); i++) {

            List<Integer> sub = new ArrayList<Integer>();

            for ( int j = 0; j<nums.length; j++) {
                if (((i>>j) & 1) == 1) {
                    sub.add(nums[j]);
                }
            }
            res.add(sub);
        }
        return res;
    }
}
相关推荐
码少女1 天前
数据结构——希尔排序
数据结构·排序算法
星子yu1 天前
【学习】怎么学好数据结构
数据结构·学习
alphaTao1 天前
LeetCode 每日一题 2026/7/6-2026/7/12
算法·leetcode
想吃火锅10051 天前
【leetcode】56.合并区间js
算法·leetcode·职场和发展
imuliuliang1 天前
可合并堆在多任务调度中的优势与实现技巧7
算法
学究天人1 天前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷6)
网络·算法·数学建模·动态规划·几何学·图论·拓扑学
wabs6661 天前
关于动态规划【力扣72.编辑距离的思考】
算法·leetcode·动态规划
用户333239768841 天前
我做了一个 RepoMind:让 AI 写架构前,先去看看真实开源项目
算法
chh5631 天前
C++--list
开发语言·数据结构·c++·学习·算法·list
killerbasd1 天前
总结 7。10
人工智能·算法·机器学习