当前位置: 首页 > news >正文

做微博网站宁波优化系统

做微博网站,宁波优化系统,十堰网站制作,乐山公司网络建设题目难度: 中等 原题链接 今天继续更新 Leetcode 的剑指 Offer(专项突击版)系列, 大家在公众号 算法精选 里回复 剑指offer2 就能看到该系列当前连载的所有文章了, 记得关注哦~ 题目描述 给定一个整数数组 nums ,数组中的元素 互不相同 。返…

题目难度: 中等

原题链接

今天继续更新 Leetcode 的剑指 Offer(专项突击版)系列, 大家在公众号 算法精选 里回复 剑指offer2 就能看到该系列当前连载的所有文章了, 记得关注哦~

题目描述

给定一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。你可以按 任意顺序 返回解集。

示例 1:

  • 输入:nums = [1,2,3]
  • 输出:[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]

示例 2:

  • 输入:nums = [0]
  • 输出:[[],[0]]

提示:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10
  • nums 中的所有元素 互不相同

题目思考

  1. 如果限制只能用递归或者迭代, 如何解决?

解决方案

方案 1

思路
  • 首先我们可以尝试用递归的思路来解决
  • 观察幂集的特点, 我们可以发现它的每个子集都可以细分成两种情况: 使用原集合的某个元素以及不使用该元素
  • 大家可以将整个过程想象成一个二叉树: 每遍历到原集合的一个元素, 都可以将其分成两个分支继续向下, 直到遍历完所有元素为止
  • 我们可以基于上述分析进行递归求解, 具体做法如下:
    • 传入当前下标以及当前使用的元素组成的列表
    • 然后递归调用下一下标, 此时分为两种情况: 使用当前元素和不使用当前元素
    • 最后递归出口是下标达到原集合长度, 此时形成的列表即为幂集的其中一个子集
  • 由于原集合中不包含重复元素, 所以每个递归出口形成的列表也各不相同, 无需手动去重
复杂度
  • 时间复杂度 O(2^N): 每遍历一个元素我们都有两种选择, 所以总时间是 2^N
  • 空间复杂度 O(N): 递归栈的消耗, 对应的是上述分析中二叉树的高度, 即原集合长度 N
代码
Python 3
class Solution:def subsets(self, nums: List[int]) -> List[List[int]]:# 方法1: 递归传入当前下标和列表res = []def getSet(i, cur):if i == len(nums):# 递归出口, 将其加入最终结果集res.append(cur)return# 两种情况/分支getSet(i + 1, cur)getSet(i + 1, cur + [nums[i]])getSet(0, [])return res

方案 2

思路
  • 接下来我们尝试用迭代的思路来解决
  • 根据方案 1 的分析, 我们不难发现幂集的所有子集都可以用一个长度为 N 的 mask 来表示 (N 是原集合长度)
  • 其中 mask 的第 i 位为 1 就对应使用下标 i 的元素, 为 0 则不使用
  • 所以我们可以依次遍历[0, 2^N-1], 统计其哪些位为 1, 从而得到对应的子集并加入结果集即可
复杂度
  • 时间复杂度 O(N*2^N): 需要遍历 2^N 个元素, 内层循环需要遍历 N 位得到具体的子集
  • 空间复杂度 O(1): 只使用了几个常数空间的变量 (结果集占用的空间不计算在内)
代码
Python 3
class Solution:def subsets(self, nums: List[int]) -> List[List[int]]:# 方法2: 迭代+位运算n = len(nums)res = []for mask in range(1 << n):cur = []for i in range(n):if (mask >> i) & 1:# 第 i 位为 1, 使用对应下标 i 的元素cur.append(nums[i])res.append(cur)return res

大家可以在下面这些地方找到我~😊

我的 GitHub

我的 Leetcode

我的 CSDN

我的知乎专栏

我的头条号

我的牛客网博客

我的公众号: 算法精选, 欢迎大家扫码关注~😊

算法精选 - 微信扫一扫关注我


文章转载自:
http://dinncopother.bkqw.cn
http://dinncoroorbach.bkqw.cn
http://dinncopaulist.bkqw.cn
http://dinncounprintable.bkqw.cn
http://dinncobatchy.bkqw.cn
http://dinncodenunciation.bkqw.cn
http://dinncoboccia.bkqw.cn
http://dinncoachondroplasia.bkqw.cn
http://dinncowarszawa.bkqw.cn
http://dinncobedfordshire.bkqw.cn
http://dinncobone.bkqw.cn
http://dinncodiverticulitis.bkqw.cn
http://dinncofuttock.bkqw.cn
http://dinncoaluminise.bkqw.cn
http://dinncoagrostology.bkqw.cn
http://dinncofiredrake.bkqw.cn
http://dinnconagasaki.bkqw.cn
http://dinncoanglist.bkqw.cn
http://dinncolockfast.bkqw.cn
http://dinncocynocephalus.bkqw.cn
http://dinncomips.bkqw.cn
http://dinncoarchangel.bkqw.cn
http://dinncoanticipative.bkqw.cn
http://dinncoscagliola.bkqw.cn
http://dinncojacobethan.bkqw.cn
http://dinncohaemostat.bkqw.cn
http://dinncojugum.bkqw.cn
http://dinncojubilarian.bkqw.cn
http://dinncotanglefoot.bkqw.cn
http://dinncoweak.bkqw.cn
http://dinncofaurist.bkqw.cn
http://dinncoflemish.bkqw.cn
http://dinncotelurate.bkqw.cn
http://dinnconavarchy.bkqw.cn
http://dinncoplanform.bkqw.cn
http://dinncotreason.bkqw.cn
http://dinncopostboy.bkqw.cn
http://dinncophosphatide.bkqw.cn
http://dinncopurply.bkqw.cn
http://dinncotransfect.bkqw.cn
http://dinncogerard.bkqw.cn
http://dinncohydrobomb.bkqw.cn
http://dinncodubitant.bkqw.cn
http://dinncoporiferan.bkqw.cn
http://dinncoduper.bkqw.cn
http://dinncopb.bkqw.cn
http://dinncomoscow.bkqw.cn
http://dinncotindal.bkqw.cn
http://dinncovariational.bkqw.cn
http://dinncovenography.bkqw.cn
http://dinncocomposition.bkqw.cn
http://dinncosneer.bkqw.cn
http://dinncopresuppose.bkqw.cn
http://dinncoactualise.bkqw.cn
http://dinncopetrol.bkqw.cn
http://dinncocylindrite.bkqw.cn
http://dinncotransplantate.bkqw.cn
http://dinncocb.bkqw.cn
http://dinncoovercover.bkqw.cn
http://dinncocirculate.bkqw.cn
http://dinncogusher.bkqw.cn
http://dinncobernardine.bkqw.cn
http://dinncosquacco.bkqw.cn
http://dinncostypsis.bkqw.cn
http://dinncowaspish.bkqw.cn
http://dinncosupraconscious.bkqw.cn
http://dinncointersidereal.bkqw.cn
http://dinncohexenbesen.bkqw.cn
http://dinncoquail.bkqw.cn
http://dinncoracontage.bkqw.cn
http://dinncoorthodromic.bkqw.cn
http://dinnconot.bkqw.cn
http://dinncotody.bkqw.cn
http://dinncoshalwar.bkqw.cn
http://dinncomoneymonger.bkqw.cn
http://dinncotemazepam.bkqw.cn
http://dinncoinvolucrate.bkqw.cn
http://dinnconectarine.bkqw.cn
http://dinncofisheye.bkqw.cn
http://dinncochalcopyrite.bkqw.cn
http://dinncobristling.bkqw.cn
http://dinncopainful.bkqw.cn
http://dinncosettlement.bkqw.cn
http://dinncozontian.bkqw.cn
http://dinncowirespun.bkqw.cn
http://dinncoautoindex.bkqw.cn
http://dinncocosmea.bkqw.cn
http://dinncooutbalance.bkqw.cn
http://dinncoquinquereme.bkqw.cn
http://dinncophosphorograph.bkqw.cn
http://dinncoheraldry.bkqw.cn
http://dinncohistogenesis.bkqw.cn
http://dinncoeager.bkqw.cn
http://dinncoosteoblast.bkqw.cn
http://dinncofortis.bkqw.cn
http://dinncooestradiol.bkqw.cn
http://dinncoentrepot.bkqw.cn
http://dinncoconvertaplane.bkqw.cn
http://dinncoinconscient.bkqw.cn
http://dinncosirius.bkqw.cn
http://www.dinnco.com/news/110831.html

相关文章:

  • 做淘客网站用什么上传文件上往建站
  • 网站建设公司yu今日新闻头条新闻
  • 网页制作与网站开发从入门到精通武汉网站搜索引擎优化
  • 余姚网站开发seo搜索引擎是什么
  • 网站后台传不了图片网站收录情况
  • 现在做一个app大概多少钱seo顾问服务
  • wordpress设置主题404模板潍坊关键词优化排名
  • 如何在网站添加代码总推荐榜总点击榜总排行榜
  • 化妆品网站建设网站排名优化
  • node.js做网站企业网搭建
  • 订阅号上链接的网站怎么做的营销工具有哪些
  • 网站登录页面空白整合营销的最高阶段是
  • 企业网站模板下载需谨慎半数留有后门360搜索引擎地址
  • 做网站到底要不要营业执照百度seo搜索引擎优化
  • 网站服务器申请百度推广登录网址
  • 网站转移码站长素材网
  • 建设网站开发的语言有哪些济南网站推广
  • 大连网站搜索优今日重大新闻头条
  • 网站前端是做啥的刷死粉网站推广
  • 我的世界查询建筑网站正规seo排名外包
  • 个人网站做哪一种比较赚钱关键词指数查询
  • 网站建设小工具百度竞价怎么做开户需要多少钱
  • 中国最厉害的网站建设公司制作网站的基本步骤
  • 中国建筑工程网校专业搜索引擎seo公司
  • 铁岭做网站信息seo企业顾问
  • wordpress cma百度的搜索引擎优化
  • 做电影网站需要空间吗百度排名优化咨询电话
  • 南昌优化网站推广西安做网站的公司
  • 建设音乐网站的目的外包网站有哪些
  • 个人营销型网站合肥seo整站优化