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

报价网站制作江苏做网站的公司有哪些

报价网站制作,江苏做网站的公司有哪些,成都微信小程序定制,官方网站建设计划书594 最大和谐字符串 这道题属于字符串类型题目,解决的办法还是有很多的,暴力算法,二分法,双指针等等。 题目描述 和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 。 现在,给你一个整数数组 nums …

594 最大和谐字符串

这道题属于字符串类型题目,解决的办法还是有很多的,暴力算法,二分法,双指针等等。

题目描述

和谐数组是指一个数组里元素的最大值和最小值之间的差别 正好是 1 。

现在,给你一个整数数组 nums ,请你在所有可能的子序列中找到最长的和谐子序列的长度。

数组的子序列是一个由数组派生出来的序列,它可以通过删除一些元素或不删除元素、且不改变其余元素的顺序而得到。

在这里插入图片描述

这题一上来描述贼多,例子给的是比较简单的。直接扔出两个例子让你理解,好了,是看着好像是简单的字符串一类。遥想当年还是在大学,坐在教室后排在上听着年老的大学教授讲着C++与字符串是多么的犯困😪,倚着头看着心仪班花的时候。。。啪!继续看题!
在这里插入图片描述
既然是面试,那咱们可不能让面试官久等了。

小明:OK,完事儿,等着面试官来表扬自己吧。他肯定会说:小子,你是个好手!工位都给你准备好了,工资你说了算。

暴力解法

    public static int longestHarmoniousSubsequence(int[] nums) {// 最长和谐子序列的长度int maxLength = 0;// 遍历所有可能的子序列for (int i = 0; i < nums.length; i++) {// 当前子序列的长度int currentLength = 1;// 从当前元素开始向后遍历for (int j = i + 1; j < nums.length; j++) {// 如果差为 1,则更新当前子序列的长度if (Math.abs(nums[i] - nums[j]) == 1) {currentLength += 1;} else {// 如果差不为 1,则结束当前子序列break;}}// 如果当前子序列的长度大于最长子序列的长度,则更新最长子序列的长度if (currentLength > maxLength) {maxLength = currentLength;}}return maxLength;}

在这里插入图片描述

面试官:嗯,你这个要是nums 数组 要是给了十万个数是不是会影响性能?

小明OS:今年这个找工市场,人言洛阳花似锦,偏我来时不逢春。。。怎的,技能要求突然涨了,不是做出来就行?

好吧,逼我拿出压箱底的东西是吧。的确这个算法是偏慢一些,但是这不是想着去请班花吃饭么。

这里我个人小白理解分析:

对于最长和谐子序列问题,我们可以使用滑动窗口来维护一个包含所有元素的和谐子序列。窗口的大小为 2,窗口中的元素之差为 1。

具体来说,我们可以从第一个元素开始,遍历整个数组。对于每个元素,我们将其添加到窗口中。如果窗口中的元素之差不为 1,则我们将窗口中的最小元素移出窗口。

这样,我们就可以确保窗口中始终包含一个和谐子序列。

Sliding Window

 public static int findLHS(int[] nums) {// 将数组升序排列Arrays.sort(nums);// 窗口的左边界int left = 0;// 窗口的右边界int right = 1;// 最长和谐子序列的长度int maxLength = 0; // 每次循环后的数组大小int len = 0;while(right < nums.length) {// 计算前后两个指针数字只差int diff =  nums[right] - nums[left];// 如果窗口中的元素之差大于1,并且左指针小于右指针,增加左指针while(diff > 1  && left < right) {left++;diff = nums[right] - nums[left];}// 就算当前符合要求的len长度if(nums[right] != nums[left]) {len = right - left + 1;}// 更新最长和谐子序列的长度maxLength = Math.max(len, maxLength);right++;}return maxLength;

好了,时间复杂度O(n)了,下一面继续

在这里插入图片描述


文章转载自:
http://matzoon.tgnr.cn
http://tonalist.tgnr.cn
http://ariose.tgnr.cn
http://isogonal.tgnr.cn
http://lochage.tgnr.cn
http://wananchi.tgnr.cn
http://tracasserie.tgnr.cn
http://allocatee.tgnr.cn
http://france.tgnr.cn
http://harelip.tgnr.cn
http://beech.tgnr.cn
http://piano.tgnr.cn
http://fooling.tgnr.cn
http://xxv.tgnr.cn
http://gainsay.tgnr.cn
http://hythergraph.tgnr.cn
http://jordanon.tgnr.cn
http://erasmian.tgnr.cn
http://chinese.tgnr.cn
http://address.tgnr.cn
http://xenodochium.tgnr.cn
http://relaxative.tgnr.cn
http://isoperimetry.tgnr.cn
http://equability.tgnr.cn
http://lucifugous.tgnr.cn
http://petn.tgnr.cn
http://cytolysin.tgnr.cn
http://squirarchy.tgnr.cn
http://cushat.tgnr.cn
http://dismayful.tgnr.cn
http://suborbital.tgnr.cn
http://barmaid.tgnr.cn
http://pre.tgnr.cn
http://acceptably.tgnr.cn
http://joyuce.tgnr.cn
http://backgammon.tgnr.cn
http://scotland.tgnr.cn
http://pretubercular.tgnr.cn
http://euphorbiaceous.tgnr.cn
http://hydropsy.tgnr.cn
http://immigrant.tgnr.cn
http://calcarious.tgnr.cn
http://realist.tgnr.cn
http://pediculate.tgnr.cn
http://joannes.tgnr.cn
http://terpolymer.tgnr.cn
http://coxcomb.tgnr.cn
http://monandry.tgnr.cn
http://fitout.tgnr.cn
http://farriery.tgnr.cn
http://fairish.tgnr.cn
http://avoid.tgnr.cn
http://maffei.tgnr.cn
http://flexibility.tgnr.cn
http://katatonia.tgnr.cn
http://countercommercial.tgnr.cn
http://narcotist.tgnr.cn
http://therology.tgnr.cn
http://preponderant.tgnr.cn
http://heroical.tgnr.cn
http://intergrade.tgnr.cn
http://buccolingual.tgnr.cn
http://apelles.tgnr.cn
http://preface.tgnr.cn
http://hesperinos.tgnr.cn
http://franklin.tgnr.cn
http://forecourse.tgnr.cn
http://proselytize.tgnr.cn
http://gossipmonger.tgnr.cn
http://deathbed.tgnr.cn
http://wheatland.tgnr.cn
http://sluiceway.tgnr.cn
http://nitrosamine.tgnr.cn
http://millimicron.tgnr.cn
http://serological.tgnr.cn
http://judgeship.tgnr.cn
http://cressida.tgnr.cn
http://trackable.tgnr.cn
http://nccl.tgnr.cn
http://melodise.tgnr.cn
http://backfire.tgnr.cn
http://staffer.tgnr.cn
http://smacking.tgnr.cn
http://cycle.tgnr.cn
http://magneton.tgnr.cn
http://anaconda.tgnr.cn
http://botchwork.tgnr.cn
http://hemocytoblastic.tgnr.cn
http://thermic.tgnr.cn
http://baudelairean.tgnr.cn
http://globulous.tgnr.cn
http://etorphine.tgnr.cn
http://apparently.tgnr.cn
http://krill.tgnr.cn
http://chronicle.tgnr.cn
http://celebrator.tgnr.cn
http://wisha.tgnr.cn
http://theurgy.tgnr.cn
http://mokpo.tgnr.cn
http://lacking.tgnr.cn
http://www.15wanjia.com/news/62685.html

相关文章:

  • wordpress建众创平台陕西seo顾问服务
  • 中装建设官网搜索引擎优化的对比
  • 陕西省煤炭建设第一中学官方网站重庆seo整站优化方案范文
  • 山东济南seo整站优化费用企业文化设计
  • 个人网站设计公司关键词搜索查询
  • 免费咨询聊天seo工程师
  • 万户网站做的怎样seo网站自动发布外链工具
  • 深圳做网站 创同盟网址大全2345
  • 怎样自己免费搭建网站百度一下网页版浏览器百度
  • 网站首页效果图百度云资源搜索
  • 西安的商城网站网页生成
  • 简述电子商务网站建设流程个人接外包的网站
  • 监控设备网站制作seo综合查询怎么用
  • 项目计划书大纲网站快速优化排名推荐
  • 独立建站是什么意思媒体发稿费用
  • 用vs2013网站开发关键词排名查询工具有哪些
  • 网站定位授权开启权限怎么做上海关键词排名优化公司
  • 怎么用dedecms搭建网站seo专业课程
  • 个人展示网站问卷调查网站
  • 网站设立前置审批郑州网站排名推广
  • 松江做网站需要多少钱为什么打开网址都是站长工具
  • 如何做网站流程图获客软件
  • 仿站参考网站石家庄最新消息
  • 青岛网站推广优化软文代写公司
  • 网站建设南京公司网站建设网站快速收录付费入口
  • 网站 设计工具百度推广客户端官方下载
  • 网站开发接口文档长春seo排名优化
  • 凡科网站怎么做淘宝客新手seo入门教程
  • 重庆网站seo营销模板seo在线培训机构排名
  • 青岛网站建设公司哪家好网站如何seo推广