弟中弟的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 ...
最新文章
- 24GHz和77GHz毫米波雷达技术细节
- 【洛谷习题】小A点菜
- git-svn:通过git来管理svn代码
- JavaCore/HeapDump文件及其分析方法
- [Android]你不知道的Android进程化(3)--进程等级
- 用odac连接oracle 12154,OracleConnection 連線出現「ORA-12154: TNS: 無法解析指定的連線 ID」...
- allocator类编程实验
- openwrt是嵌入式linux,非常方便的OpenWrt的嵌入式Linux开发环境
- java组合与继承始示例_Java 8特性与示例
- paip兼容windows与linux的java类根目录路径的方法
- 操作系统实验报告,适用与大一大二学生
- Drools规则引擎使用
- android ca,Android CA证书安装流程
- 如何批量预测lncRNA靶向的miRNA?
- windows中的SC命令
- python修改微信和支付宝步数
- dB(分贝)定义及其应用
- [贪心]leetcode55:跳跃游戏(medium)
- linux中ret2libc入门与实践
- Activiti 5 提示:Default sequenceflow has a condition, which is not allowed