回溯-子集

78.子集

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

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

输入 :整型数组
输出 :二元列表
思路:利用二进制,(比如说数组长度为3)000、001、010、011、100、101、110、111刚好可以遍历所有情况

java 复制代码
class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> tempList = new ArrayList<>();
    public List<List<Integer>> subsets(int[] nums) {
        int n = nums.length;
        for(int i = 0; i < (1 << n); i++){
            tempList.clear();
            for(int j = 0; j < n; j++){
                if((i & (1 << j)) != 0){
                    tempList.add(nums[j]);
                }
            }
            result.add(new ArrayList<>(tempList));
        }
        return result;
    }
}
相关推荐
宸津-代码粉碎机15 小时前
FastUtil+AI多Agent实战:Java AI项目性能终极加速方案
java·服务器·开发语言·python·安全·php
_Narcissus_15 小时前
二分算法笔记及例题
数据结构·c++·笔记·算法·蓝桥杯·查找·二分算法
tachibana215 小时前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm
choumou_M15 小时前
SpringBoot_5:商品秒杀场景的并发实践(初步)
java·spring boot·后端
suaizai_16 小时前
AI Agent如何懂你:四层能力拆解
java·前端·人工智能
javachen__16 小时前
Docker一键清理 + 全局限制,告别日志报满
java·docker·容器
qq_4195630916 小时前
ToT 的 BFS/DFS 有个致命缺口:蒙特卡洛树搜索(MCTS)用「随机试错+统计」让大模型想得更深,小模型 + 它竟超过 GPT-4
算法·深度优先·宽度优先
早安试言16 小时前
maven安装
java·maven
攻城有术17 小时前
专项攻克-SpringBoot启动后 Bean的实例数量问题
java·spring boot·后端
步行cgn17 小时前
PageHelper 的用法
java·后端