import java.util.ArrayList;
/**
* @author xienl
* @description 集合的所有子集(一)
* @date 2022/7/5
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
int[] arr = {1,2,3};
System.out.println(solution.subsets(arr).toString());
}
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
bfs(new ArrayList<>(), S, 0);
return res;
}
private void bfs(ArrayList<Integer> list, int[] s, int n){
if (n > s.length - 1){
res.add(new ArrayList<>(list));
return;
}
bfs(list, s, n + 1);
list.add(s[n]);
bfs(list, s, n + 1);
list.remove(list.size() - 1);
}
}
牛客网:NC27 集合的所有子集(一)
最新推荐文章于 2026-06-22 19:04:06 发布
本文介绍了一种使用Java实现的生成集合所有子集的方法。通过递归的方式,该算法能够输出给定数组的所有可能子集组合。代码示例清晰地展示了如何初始化数据结构并调用方法来获取结果。


617

被折叠的 条评论
为什么被折叠?



