LeetCode78 子集 2018.10.24
阿新 • • 發佈:2018-12-17
題幹:
給定一組不含重複元素的整數陣列 nums,返回該陣列所有可能的子集(冪集)。
說明:解集不能包含重複的子集。
示例:
輸入: nums = [1,2,3] 輸出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
演算法思想:與77題大致相同
class Solution: def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] """ result = [] temp = [] length = len(nums) result = self.helper(0, length, nums, temp, result) return result def helper(self, level, len, nums, temp, result): for i in range(level, len, 1): temp.append(nums[i]) result.append(temp[:]) self.helper(i + 1, len, nums, temp, result) temp.pop() #result.append([]) return result s = Solution() res = s.subsets([1, 2, 3]) print(res)