弟中弟的Leetcode总结——数组类(九)

题目描述

Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

思路

题目是求出给定元素的所有子集。与之前做过的combination sum类似,使用回溯法,走遍所有可能的路径。注意的是需要在规定不同长度的集合的条件下分别进行回溯。

代码(py3)

class Solution:def backtrack(self,nums,contain,tempset,ans,start):#如果长度够了,直接添加到最终结果中if(contain==0):t=tempset[:]ans.append(t)returnelse:#进行回溯for i in range(start,len(nums)):tempset.append(nums[i])self.backtrack(nums,contain-1,tempset,ans,i+1)tempset.pop()def subsets(self, nums):""":type nums: List[int]:rtype: List[List[int]]"""ans=[[]]for i in nums:ans.append([i])leng=len(nums)visit=[0]*leng#确定子集的长度for i in range(1,len(nums)):temp=[]#按照长度进行回溯self.backtrack(nums,i+1,temp,ans,0)return ans

弟中弟的Leetcode总结——数组类(九)相关推荐

  1. 弟中弟的Leetcode总结——数组类(十一)

    弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...

  2. 弟中弟的Leetcode总结——数组类(十)

    弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...

  3. 弟中弟的Leetcode总结——数组类(五)

    弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...

  4. 弟中弟的Leetcode总结——数组类(二)

    弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...

  5. 弟中弟的Leetcode总结——数组类(四)

    弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...

  6. 弟中弟的Leetcode总结——数组类(八)

    弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...

  7. 弟中弟的Leetcode总结——数组类(六)

    弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...

  8. 弟中弟的Leetcode总结——数组类(七)

    弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...

  9. 弟中弟的Leetcode总结——字符串类(一)

    弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...

最新文章

  1. 1578: [Usaco2009 Feb]Stock Market 股票市场
  2. spring_有关Spring缓存性能的更多信息
  3. linux底层硬件需求,Linux系统的硬件设备驱动的底层结构讲解
  4. Wire:Linux开源聊天应用
  5. php只显示指定文件类型_php强制下载指定类型的文件
  6. Oracle 数据库表空间不足拓展方法实例演示,表空间剩余大小查看,通过新增表空间文件拓展表空间,表空间文件路径查看
  7. C#中new和override的区别
  8. PythonGIS可视化—Matplot basemap工具箱
  9. java 高效文本查找替换_Java 查找、高亮PDF 文本
  10. 对VSCode在安装了Dev-cpp的电脑上的配置
  11. python机器学习案例系列教程——逻辑分类/逻辑回归LR/一般线性回归(softmax回归)
  12. 力扣-746. 使用最小花费爬楼梯
  13. Redis之连接redis服务命令
  14. 解决office2016显示图标异常
  15. 计算机网络拓扑结构的分析,计算机网络拓扑结构分析
  16. 如何隐藏QDockWidget的标题栏
  17. 2010年全球移动行业回顾:iPad大热 中国崛起
  18. 阿里云 Elasticsearch简介和购买流程
  19. Android音视频——基础介绍
  20. 连快播王欣都要做区块链,蚂蚁金服为什么不碰ICO?

热门文章

  1. 3dmax模型导出成glb格式供Cesium使用
  2. python爬虫教程:Scrapy框架爬取Boss直聘网Python职位信息的源码
  3. 图书管理系统——图书的查询功能/图书的添加功能
  4. 计算机教室logo,小学信息技术LOGO语言教学法.doc
  5. C++麻将游戏算法深入解析
  6. spring事务之事务挂起和事务恢复源码
  7. pc端网页的设计尺寸
  8. python绘制彩色蟒蛇 带注释
  9. 初识Quartz之Job组件
  10. 基于PandO MPPT的光伏阵列系统simulink仿真包括VSC控制器,PandO MPPT以及Utility电网