1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| class (object): def subsetsWithDup(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ """ 思路整理:DFS recursion 1)对nums进行排序以避免nums中重复元素不聚集在一起如[1,2,4,4,3,4] 2)从nums中依次取出元素加到path中进行DFS 3)对于重复元素如[1,2,2]中的2,res中每个以2为开头的数组的只取nums的第一个2 Explanation 1)sort nums to gather all same number together 2)take numbers from nums iteratively and use them to conduct DFS 3)for same numbers like 2 in [1,2,2],each array only take the first 2 as its first 2 """ def helper(start,end,path,res): res.append(path) for i in range(start,end): if i!=start and nums[i]==nums[i-1]:continue helper(i+1,end,path+[nums[i]],res) return res=[] nums.sort() helper(0,len(nums),[],res) return res< 大专栏 LeetCode Problem 90. Subsets IIbr/> |