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

热门文章

  1. matlab中subplot函数的功能
  2. 六轴机械臂机械臂人脸识别和跟踪
  3. 在Eclipse上调试Spark项目
  4. 阶段01-html和css基础(总结04)
  5. Win10系统路由器挂载的硬盘无法打开的解决方法
  6. guestbook.php,guestbook.php
  7. 【大牛系列教学】java在线答题系统
  8. 最全的OLT设备介绍
  9. 大阪第47天——老徐也出字体了
  10. 秒懂集成商,分销商和代理商的区别