Leetcode 08.04 幂集 回溯与位图
比较常规的解法为回溯解法:
List<List<Integer>> reList = new LinkedList<List<Integer>>(); public final List<List<Integer>> subsets1(int[] nums) { reList.add(new ArrayList<Integer>()); Stack<Integer> stack = new Stack<Integer>(); //所有可能长度 for (int i = 1; i <= nums.length; i++) { //所有可能头元素 for (int j = 0; j <= nums.length - i; j++) { subWay(nums, j, i, stack); } } return reList; } /** * @Author Niuxy * @Date 2020/7/21 12:26 下午 * @Description 遍历已 flag 为头元素,长度为 length 的所有可能子序列 */ private final void subWay(int[] nums, int flag, int length, Stack<Integer> stack) { if (length == 1) { if (flag < nums.length) { stack.push(nums[flag]); reList.add(new ArrayList<Integer>(stack)); stack.pop(); } return; } if (length > nums.length - flag || flag > nums.length) { return; } stack.push(nums[flag]); for (int i = flag + 1; i < nums.length; i++) { subWay(nums, i, length - 1, stack); } stack.pop(); }
除此之外还有一种取巧的方法:位图。
每个自然数都是独一无二的,它们的二进制表示形式也是。
对于 n 个元素的输入,将会有 二的 N 次方(设为 k )种排列组合方式。
0 的二进制表示为 00000000 000000000 00000000 00000000
1 的二进制表示为 00000000 000000000 00000000 00000001
2 的二进制表示为 00000000 00000000 00000000 00000010
对于每个数的二进制表示,对应到输入的数组下标,1 表示选择该元素,0 表示不选择该元素。
则从 1 到 k 的位图可以完全的表示所有可能的组合。
位图解法:
/** * @Author Niuxy * @Date 2020/7/21 12:31 下午 * @Description 位图解法 */ public final List<List<Integer>> subsets(int[] nums) { int source = (int) Math.pow(2, nums.length); List<List<Integer>> reList = new LinkedList<List<Integer>>(); for (int i = 1; i <= source; i++) { List<Integer> inList = new LinkedList<Integer>(); for (int j = 0; j < nums.length; j++) { if (((i >>> j) & 1) == 1) { inList.add(nums[j]); } } reList.add(inList); } return reList; }
位图解法效果:
当你看清人们的真相,于是你知道了,你可以忍受孤独
分类:
数据结构与算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构