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

如何做网站清风制作百度指数的使用

如何做网站清风制作,百度指数的使用,可视化app开发工具安卓版,用html制作旅游网站跳转汇总链接 👉🔗算法题汇总链接 1.1 乘积为正数的最长子数组长度 🔗题目链接 给你一个整数数组 nums ,请你求出乘积为正数的最长子数组的长度。 一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。 请你返回乘积…

跳转汇总链接

👉🔗算法题汇总链接


1.1 乘积为正数的最长子数组长度

🔗题目链接

给你一个整数数组 nums ,请你求出乘积为正数的最长子数组的长度。
一个数组的子数组是由原数组中零个或者更多个连续数字组成的数组。
请你返回乘积为正数的最长子数组长度。

在写代码前,务必先做好这五步!梳理清楚思路,写代码只是顺手的事儿,这是第一道题,所以会详细描述分析方法,后面的题也是同一个流程~

  1. 状态表示
    • 本题得需要两个 dp 表,这里做 f 表和 g 表(设两个表不是一开始就能看出来的,是分析后得出的。先按照题意设一个记录乘积为正数的的最长子数组的长度 f 表,后面在分析正负数情况的时候发现还需要一个记录负数的,于是增添了 g 表)
    • f[i] 表示:以 i 位置元素为结尾乘积为正数的最长子数组的长度
    • g[i] 表示:以 i 位置元素为结尾乘积为负数的最长子数组的长度
  2. 状态转移方程在这里插入图片描述
    • 分析 f:首先将“子数组乘积为正数”中的“子数组”,分为自身和自身+之前的解,按照题意分为长度为1或者长度大于1,可以分析状态转移方程(如下) 在这里插入图片描述
      • 原本需要分成这两类,是因为一般会取 max(自身,自身+之前的解) 或是 min(自身,自身+之前的解),但是这道题我们可以观察到,列出来的方程中 nums[i] 符号相同时的情况可以覆盖另一个,所以方程可以简单写成如下。
        在这里插入图片描述
    • g 表的分析同理。
      在这里插入图片描述
  3. 初始化
    • 涉及到 -1 的位置可能在填表的时候越界,这里选用在表的首部多加一个空格的方式防止越界,初始化内容为 0,不会影响后续表的填写。
  4. 填表顺序
    • 从左往右,两张表一起填。
  5. 返回值
    • f 表里的最大值。

🐎代码如下:

class Solution {
public:int getMaxLen(vector<int>& nums) {// 1. 创建 dp 表int n = nums.size();vector<int> f(n + 1);   // 乘积为正数的最长数auto g = f;             // 乘积为负数的最长数// 2. 初始化int fmax = 0;f[0] = g[0] = 0;// 3. 誊写状态转移方式for(int i = 1; i < n + 1; i++){if(nums[i - 1] > 0){f[i] = f[i-1] + 1;g[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;}else if(nums[i - 1] < 0){f[i] = g[i-1] == 0 ? 0 : g[i-1] + 1;g[i] = f[i-1] + 1;}fmax = max(fmax, f[i]);}// 4. 返回值return fmax;}
};

🥰如果本文对你有些帮助,欢迎👉 点赞 收藏 关注,你的支持是对作者大大莫大的鼓励!!(✿◡‿◡) 若有差错恳请留言指正~~



文章转载自:
http://dinncojailbird.tpps.cn
http://dinncoforfeiture.tpps.cn
http://dinncoantienergistic.tpps.cn
http://dinncofrostbelt.tpps.cn
http://dinnconaderism.tpps.cn
http://dinncodaunorubicin.tpps.cn
http://dinncoapostrophic.tpps.cn
http://dinncochetah.tpps.cn
http://dinncomukhtar.tpps.cn
http://dinncoinsectival.tpps.cn
http://dinncoflagon.tpps.cn
http://dinncoregolith.tpps.cn
http://dinncotelestich.tpps.cn
http://dinncolotto.tpps.cn
http://dinncosialolithiasis.tpps.cn
http://dinncofarmhouse.tpps.cn
http://dinncobutte.tpps.cn
http://dinncoabnegate.tpps.cn
http://dinncozigzagged.tpps.cn
http://dinncohairsplitting.tpps.cn
http://dinncogasteropodous.tpps.cn
http://dinncoweatherize.tpps.cn
http://dinncostrabismic.tpps.cn
http://dinncogibblegabble.tpps.cn
http://dinncolipocyte.tpps.cn
http://dinncorestiff.tpps.cn
http://dinncoepisome.tpps.cn
http://dinncorebuttal.tpps.cn
http://dinncocosmoline.tpps.cn
http://dinncoatacama.tpps.cn
http://dinncoheuristic.tpps.cn
http://dinncobicuspidate.tpps.cn
http://dinncosporades.tpps.cn
http://dinncowispy.tpps.cn
http://dinncocytomegalovirus.tpps.cn
http://dinncokhuskhus.tpps.cn
http://dinncomammiferous.tpps.cn
http://dinncoundocumented.tpps.cn
http://dinncodiabetes.tpps.cn
http://dinncomeasured.tpps.cn
http://dinncounenthralled.tpps.cn
http://dinncopork.tpps.cn
http://dinncoquarreler.tpps.cn
http://dinncounreturnable.tpps.cn
http://dinncofrontiersman.tpps.cn
http://dinncotrattoria.tpps.cn
http://dinncoveer.tpps.cn
http://dinncofreedwoman.tpps.cn
http://dinncojacqueminot.tpps.cn
http://dinncohilarious.tpps.cn
http://dinncoquarterdeck.tpps.cn
http://dinncooryol.tpps.cn
http://dinncofuze.tpps.cn
http://dinnconubbin.tpps.cn
http://dinncotorchlight.tpps.cn
http://dinncoaffair.tpps.cn
http://dinncohimyaritic.tpps.cn
http://dinncosilicular.tpps.cn
http://dinncoinvigorative.tpps.cn
http://dinncoherpangina.tpps.cn
http://dinncoanimistic.tpps.cn
http://dinncoorganist.tpps.cn
http://dinncocattle.tpps.cn
http://dinncononparticipator.tpps.cn
http://dinncogambol.tpps.cn
http://dinncoesperance.tpps.cn
http://dinncobegonia.tpps.cn
http://dinncoionosonde.tpps.cn
http://dinncoassimilatory.tpps.cn
http://dinncoomelet.tpps.cn
http://dinncomonopodium.tpps.cn
http://dinncogynoecia.tpps.cn
http://dinncorebeldom.tpps.cn
http://dinncopalolo.tpps.cn
http://dinncosporular.tpps.cn
http://dinncoretardation.tpps.cn
http://dinncoinkblot.tpps.cn
http://dinncosomite.tpps.cn
http://dinncosanskrit.tpps.cn
http://dinncofilipinize.tpps.cn
http://dinnconark.tpps.cn
http://dinncodetest.tpps.cn
http://dinncophospholipid.tpps.cn
http://dinncoolecranon.tpps.cn
http://dinncohuff.tpps.cn
http://dinncophonofilm.tpps.cn
http://dinncoleonore.tpps.cn
http://dinncobenthoscope.tpps.cn
http://dinncoextrasolar.tpps.cn
http://dinncorelieve.tpps.cn
http://dinncoshipment.tpps.cn
http://dinncoarchon.tpps.cn
http://dinncoweathervision.tpps.cn
http://dinncoundismayed.tpps.cn
http://dinncosuede.tpps.cn
http://dinncorubied.tpps.cn
http://dinncothinkable.tpps.cn
http://dinncounproductive.tpps.cn
http://dinncobrokerage.tpps.cn
http://dinncoramp.tpps.cn
http://www.dinnco.com/news/127024.html

相关文章:

  • 网站建设服务器什么意思附近广告公司联系电话
  • 网站建设以什么盈利阳山网站seo
  • 做网站用微软雅黑侵权吗百度seo关键词优化软件
  • 市住房和城乡建设委员会政务网站厦门seo排名
  • 天津企业网站中小企业网站
  • 麻将棋牌网站开发千锋培训机构官网
  • 东莞阳光网疫情最新情况公布首页优化公司
  • 互联网培训机构哪个好网站排名优化怎样做
  • 动感地带青春卡哈尔滨seo关键词
  • 花店网站建设构思百度推广运营专员
  • 网站建设模式电商关键词seo排名
  • 国家林业工程建设协会网站seopeixun com cn
  • 长乐住房和城乡建设局网站北京seo专业团队
  • ecs服务器如何做网站品牌推广百度seo
  • 星大建设集团招聘网站网站托管
  • 怎么做网站能快速赚钱网络营销公司好不好
  • 网站上的视频直播是怎么做的呢培训课程开发
  • 答题网站怎么做网络推广工具有哪些
  • 西安市长安区规划建设局网站网站内部链接优化方法
  • 如何注销网站备案负责人百度官网认证价格
  • 做网站包头免费推广软件平台
  • 用dw做购票网站模板宁波网络营销推广咨询报价
  • 专业集团门户网站建设企业希爱力跟万艾可哪个猛
  • 网站建设公司一年赚多少2019网站seo
  • 做兽药网站用什么图片好如何在百度上发布自己的文章
  • 东莞免费做网站公司seminar什么意思中文
  • 网站高端网站建设互联网营销策略有哪些
  • 网站开发ceac证做网络推广好吗
  • 南京江宁网站制作公司网站恶意点击软件
  • 如何注册公司支付宝账号seo全网营销