回溯-子集

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;
    }
}
相关推荐
程途知微3 分钟前
ConcurrentHashMap线程安全实现原理全解析
java·后端
Mars酱8 分钟前
1分钟编写贪吃蛇 | JSnake贪吃蛇单机版
java·后端·开源
devpotato9 分钟前
人工智能(四)- Function Calling 核心原理与实战
java·人工智能
田梓燊11 分钟前
2026/4/11 leetcode 3741
数据结构·算法·leetcode
默 语16 分钟前
Records、Sealed Classes这些新特性:Java真的变简单了吗?
java·开发语言·python
斯内科20 分钟前
FFT快速傅里叶变换
算法·fft
zjshuster25 分钟前
墨西哥中央银行网联清算系统接入总结
java·财务对账
小锋java123426 分钟前
SpringBoot 4 + Spring Security 7 + Vue3 前后端分离项目设计最佳实践
java·vue.js·spring boot
一 乐27 分钟前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统
葳_人生_蕤27 分钟前
hot100——栈和队列
数据结构