弟中弟的Leetcode总结——数组类(九)
弟中弟的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总结——数组类(九)相关推荐
- 弟中弟的Leetcode总结——数组类(十一)
弟中弟的Leetcode总结--数组类(十一) 题目描述 Single Number Given a non-empty array of integers, every element appear ...
- 弟中弟的Leetcode总结——数组类(十)
弟中弟的Leetcode总结--数组类(十) 题目描述 Climbing Stairs You are climbing a stair case. It takes n steps to reach ...
- 弟中弟的Leetcode总结——数组类(五)
弟中弟的Leetcode总结--数组类(五) 题目描述 Remove Duplicates from Sorted Array Given a sorted array nums, remove th ...
- 弟中弟的Leetcode总结——数组类(二)
弟中弟的Leetcode总结--数组类(二) 题目描述 Container With Most Water Given n non-negative integers a1, a2, -, an , ...
- 弟中弟的Leetcode总结——数组类(四)
弟中弟的Leetcode总结--数组类(四) 题目描述 Two Sum Given an array of integers, return indices of the two numbers su ...
- 弟中弟的Leetcode总结——数组类(八)
弟中弟的Leetcode总结--数组类(八) 题目描述 Combination Sum Given a set of candidate numbers (candidates) (without d ...
- 弟中弟的Leetcode总结——数组类(六)
弟中弟的Leetcode总结--数组类(六) 题目描述 3Sum Closest Given an array nums of n integers and an integer target, fi ...
- 弟中弟的Leetcode总结——数组类(七)
弟中弟的Leetcode总结--数组类(七) 题目描述 Maximum Subarray Given an integer array nums, find the contiguous subarr ...
- 弟中弟的Leetcode总结——字符串类(一)
弟中弟的Leetcode总结--字符串类(一) 题目描述 Longest Substring Given a string, find the length of the longest substr ...
最新文章
- 1578: [Usaco2009 Feb]Stock Market 股票市场
- spring_有关Spring缓存性能的更多信息
- linux底层硬件需求,Linux系统的硬件设备驱动的底层结构讲解
- Wire:Linux开源聊天应用
- php只显示指定文件类型_php强制下载指定类型的文件
- Oracle 数据库表空间不足拓展方法实例演示,表空间剩余大小查看,通过新增表空间文件拓展表空间,表空间文件路径查看
- C#中new和override的区别
- PythonGIS可视化—Matplot basemap工具箱
- java 高效文本查找替换_Java 查找、高亮PDF 文本
- 对VSCode在安装了Dev-cpp的电脑上的配置
- python机器学习案例系列教程——逻辑分类/逻辑回归LR/一般线性回归(softmax回归)
- 力扣-746. 使用最小花费爬楼梯
- Redis之连接redis服务命令
- 解决office2016显示图标异常
- 计算机网络拓扑结构的分析,计算机网络拓扑结构分析
- 如何隐藏QDockWidget的标题栏
- 2010年全球移动行业回顾:iPad大热 中国崛起
- 阿里云 Elasticsearch简介和购买流程
- Android音视频——基础介绍
- 连快播王欣都要做区块链,蚂蚁金服为什么不碰ICO?