回溯-子集

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;
    }
}
相关推荐
梦幻通灵1 分钟前
Java中finally失效的几种情况【持续更新】
java·开发语言
雨落在了我的手上3 分钟前
Java数据结构(八):双链表的实现
数据结构
月落归舟6 分钟前
SpringMVC 多种响应返回方式
java·开发语言
吃饱了得干活12 分钟前
JVM垃圾回收:从新生代到ZGC,从理论到调优
java·jvm·后端
长不胖的路人甲19 分钟前
二叉排序树(BST)Java 完整实现 + 删除思路详解
java·开发语言·算法
geovindu20 分钟前
python: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·python·算法·搜索算法
大模型码小白25 分钟前
Java 部署:Jenkins Pipeline 构建 Java 项目(自动化)
java·人工智能·python·机器学习
长不胖的路人甲28 分钟前
可达性分析法(根搜索算法)完整详解
java·jvm·算法
笨笨饿33 分钟前
#102_Codex无在VSCold无法打开
java·c语言·数据库·笔记
paeamecium37 分钟前
【PAT甲级真题】- Kuchiguse (20)
数据结构·c++·python·算法·pat考试·pat