Leetcode: Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
Adopted approach: Notice that in line 11, you should create a copy of current path, and add it to res. Otherwise, it'll be edited later, and be wiped out to []. So the res will be [[], [], [], [], [], ...]
1 class Solution { 2 public List<List<Integer>> subsets(int[] nums) { 3 List<List<Integer>> res = new ArrayList<>(); 4 List<Integer> path = new ArrayList<>(); 5 backtracking(path, res, nums, 0); 6 return res; 7 } 8 9 public void backtracking(List<Integer> path, List<List<Integer>> res, int[] nums, int pos) { 10 if (pos == nums.length) { 11 res.add(new ArrayList<Integer>(path)); 12 return; 13 } 14 // not to add current element to the subset 15 backtracking(path, res, nums, pos + 1); 16 17 // add current element to the subset 18 path.add(nums[pos]); 19 backtracking(path, res, nums, pos + 1); 20 path.remove(path.size() - 1); 21 } 22 }
第二遍做法:
1 public List<List<Integer>> subsets(int[] nums) { 2 List<List<Integer>> list = new ArrayList<>(); 3 Arrays.sort(nums); 4 backtrack(list, new ArrayList<>(), nums, 0); 5 return list; 6 } 7 8 private void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start){ 9 list.add(new ArrayList<>(tempList)); 10 for(int i = start; i < nums.length; i++){ 11 tempList.add(nums[i]); 12 backtrack(list, tempList, nums, i + 1); 13 tempList.remove(tempList.size() - 1); 14 } 15 }
网上看到Bit Manipulation做法:
Take = 1
Dont take = 0
0) 0 0 0 -> Dont take 3 , Dont take 2 , Dont take 1 = { }
1) 0 0 1 -> Dont take 3 , Dont take 2 , take 1 = {1 }
2) 0 1 0 -> Dont take 3 , take 2 , Dont take 1 = { 2 }
3) 0 1 1 -> Dont take 3 , take 2 , take 1 = { 1 , 2 }
4) 1 0 0 -> take 3 , Dont take 2 , Dont take 1 = { 3 }
5) 1 0 1 -> take 3 , Dont take 2 , take 1 = { 1 , 3 }
6) 1 1 0 -> take 3 , take 2 , Dont take 1 = { 2 , 3 }
7) 1 1 1 -> take 3 , take 2 , take 1 = { 1 , 2 , 3 }
1 public class Solution { 2 public List<List<Integer>> subsets(int[] nums) { 3 List<List<Integer>> res = new ArrayList<>(); 4 if (nums==null || nums.length==0) return res; 5 Arrays.sort(nums); 6 int totalNum = 1<<nums.length; 7 for (int i=0; i<totalNum; i++) { //one i correspond to one subset pattern 8 List<Integer> path = new ArrayList<>(); 9 for (int k=0; k<nums.length; k++) { 10 if (((i>>k)&1) == 1) 11 path.add(nums[k]); 12 } 13 res.add(new ArrayList<Integer>(path)); 14 } 15 return res; 16 } 17 }
分类:
Leetcode
【推荐】国内首个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应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架