济南专业的设计网站,北京网站改版报价,平面设计要素,做网站百科78 子集 由题意可知数组中的元素互不相同#xff0c;所以在dfs中我们可以将当前的path直接加入到res中。 class Solution {ListListIntegerres new ArrayList();ListIntegerpath new LinkedList();public ListListInteger…78 子集 由题意可知数组中的元素互不相同所以在dfs中我们可以将当前的path直接加入到res中。 class Solution {ListListIntegerres new ArrayList();ListIntegerpath new LinkedList();public ListListInteger subsets(int[] nums) {dfs(0,nums);return res;}private void dfs(int cnt,int[] nums){res.add(new LinkedList(path));for(int i cnt;i nums.length;i){path.add(nums[i]);dfs(i 1,nums);path.remove(path.size() - 1);}}
}
时间复杂度O(n×)
空间复杂度O(n)
90 子集||
class Solution {ListListIntegerres new ArrayList();ListIntegerpath new LinkedList();public ListListInteger subsetsWithDup(int[] nums) {Arrays.sort(nums);dfs(0,nums);return res;}private void dfs(int cnt,int nums[]){res.add(new LinkedList(path));for(int i cnt;i nums.length;i){if(i cnt nums[i] nums[i - 1])continue;path.add(nums[i]);dfs(i 1,nums);path.remove(path.size() - 1);}}
}
时间复杂度O(n×)
空间复杂度O(n)
93 复原IP地址