标签:copy inpu url deepcopy htm style 题目 loop ble
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ]
import copy class Solution: def subsets(self, nums: ‘List [int]‘) -> ‘List [ List [int] ]‘ : ans = [] def helper(nums, ans, temp, pos): ans.append(copy.deepcopy(temp)) for i in range(pos, len(nums)): if i > pos and nums[i] == nums[i - 1]: continue temp.append(nums[i]) helper(nums, ans, temp, i + 1) temp.pop() nums.sort() helper(nums, ans, [], 0) return ans
class Solution: def subsets(self, nums: ‘List [int]‘) -> ‘List [ List [int] ]‘ : ans = [] def helper(nums, ans, temp, pos): ans.append(temp) for i in range(pos, len(nums)): if i > pos and nums[i] == nums[i - 1]: continue helper(nums, ans, temp + [nums[i]], i + 1) nums.sort() helper(nums, ans, [], 0) return ans
[LeetCode] 90.Subsets II tag: backtracking
标签:copy inpu url deepcopy htm style 题目 loop ble
原文地址:https://www.cnblogs.com/Johnsonxiong/p/10687993.html