当前位置: 首页 > 后端技术 > Python

力扣78.子集Python实现

时间:2023-03-26 19:47:07 Python

题目要求:思路:定义一个res保存结果集来遍历数组,将当前值添加到res结果集中的所有值中然后追加到结果集中,遍历后array,resappendsanother[[]]是结果的核心代码:res=[[]]foriinrange(len(nums)):forsubresinres[:]:res.append(subres+[nums[i]])returnrescomplete代码:classSolution(object):defsubsets(self,nums):""":typenums:List[int]:rtype:List[List[int]]"""res=[[]]foriinrange(len(nums)):forsubresinres[:]:res.append(subres+[nums[i]])返回res