回溯-子集

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;
    }
}
相关推荐
弈栈录27 分钟前
Java AI 应用的异步化与高并发设计
java·后端·架构
落魄实习生40 分钟前
Agent Scope Java 2.x 系列【2】 ReActAgent
java·开发语言
艸肅1 小时前
Ubuntu 安装 JDK (含手动安装)
java·ubuntu·jdk
Logic1011 小时前
C语言/数据结构位运算题解:异或XOR找出多任务下载器中的“独特下载速度“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
m0_734172421 小时前
Python字典计数怎样处理首次出现的键
java
卓怡学长1 小时前
w204基于springbo旅行社签证代理服务系统
java·spring boot·spring·intellij-idea
Discipline10291 小时前
尼泊尔河流流量:天气驱动的预测与高流量风险
人工智能·算法
泡海椒1 小时前
趋势分析报表:jquick-pdf折线图PDF生成实战
java·大数据·运维·服务器·前端·pdf
Json____2 小时前
家居装修 AI 智能咨询助手:让装修咨询从“大海捞针“变成“一问即答“
java·后端·vue3·it学习·wwwoop.com
vipxieliang2 小时前
ValidX 在微服务架构中的验证策略
java·spring boot