php网站设计人员,5g全连接工厂建设指南,做小程序怎么赚钱,江门论坛建站模板给你一个整数数组 nums #xff0c;数组中的元素 互不相同 。返回该数组所有可能的 子集#xff08;幂集#xff09;。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例 1#xff1a; 输入#xff1a;nums [1,2,3] 输出#xff1a;[[],[1],[2],[1,2],[3],… 给你一个整数数组 nums 数组中的元素 互不相同 。返回该数组所有可能的 子集幂集。 解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。 示例 1 输入nums [1,2,3] 输出[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] 数据结构递归栈 算法按数组顺序进行递归每次的结果都保存下来然后进行回溯继续递归 本题不难但是长时间没做和全排列搞混淆了这里不需要判断当前数是否在本次递归中是否使用因为每次进递归都是从上次数的下一个进的。
class Solution {// private int[] flag;ListListInteger list new ArrayList();ListInteger list1 new ArrayList();public ListListInteger subsets(int[] nums) {// flag new int[nums.length];dfs(nums,0);return list;}void dfs(int[] nums,int start){list.add(new ArrayList(list1));if(start nums.length)return;for(int i start; i nums.length; i){list1.add(nums[i]);dfs(nums,i1);list1.remove(list1.size()-1);} }
}