leetcode面试题 08.04. 幂集(递归)
2020-08-01 08:56
906 查看
幂集。编写一种方法,返回某集合的所有子集。集合中不包含重复的元素。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
代码
class Solution { List<List<Integer>> ress=new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { sub(nums,0); return ress; } public void sub(int[] nums,int loc) { List<List<Integer>> ss=new ArrayList<>(); if(loc==nums.length)//递归边界 { ress.add(new ArrayList<>()); return ; } sub(nums, loc+1); List<List<Integer>> next=new ArrayList<>(ress); for(List<Integer> l:ress)//将后面的子集和当前的连接 { ArrayList<Integer> na=new ArrayList<>(l); na.add(nums[loc]); next.add(new ArrayList<>(na)); } ress=next;//刷新子集结果 } }
相关文章推荐
- LeetCode :面试题64. 求1+2+…+n(递归)
- LeetCode 100 Same Tree (递归)
- * Leetcode 236. Lowest Common Ancestor of a Binary Tree 精巧的递归写法
- [leetcode/lintcode 题解]字节跳动面试题:有序数组的平方
- leetcode 二叉树非递归遍历
- [LeetCode]--401. Binary Watch(递归有点懵)
- leetcode 154. Find Minimum in Rotated Sorted Array II(二分+递归)
- 算法总结(7)--leetcode上的递归,BFS,DFS思考
- Two Sum --- LeetCode 面试题
- 【递归入门】面试题07.重建二叉树
- LeetCode 206 Reverse Linked List(反转链表)(四步将递归改写成迭代)(*)
- leetcode 35. Search Insert Position-二分查找|递归|非递归
- python leetcode38 递归
- [LeetCode]Candy 递归
- 算法面试题-leetcode学习之旅(一)
- leetcode(面试题53 - I)在排序数组中查找数字 I
- 面试题31:连续子数组的最大和(Leetcode-53:Maximum Subarray)
- 牛客剑指Offer面试题48:最长不含重复字符的子字符串(与LeetCode第三题要求不同)
- Leetcode 105:前序与中序遍历序列构造二叉树(递归+哈希提升效率)
- [Leetcode 每日精选](本周主题-设计) 面试题41. 数据流中的中位数