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

wordpress4.6手册 chm北京网站优化推广公司

wordpress4.6手册 chm,北京网站优化推广公司,邵阳建设网站公司,泉州网上办事大厅之前写过一篇这个题的,但是可能代码比较复杂,这回来个简洁版的,这个是递归版本 可以看看之前的版本,两个版本面试用哪个都保过 解法都在代码里,不懂就留言或者私信 class Solution {/**对于之前的解法,我…

之前写过一篇这个题的,但是可能代码比较复杂,这回来个简洁版的,这个是递归版本

可以看看之前的版本,两个版本面试用哪个都保过

解法都在代码里,不懂就留言或者私信

class Solution {/**对于之前的解法,我现在提供一共更优的解,但是这种可能会比较难懂一些(思想方面)代码其实是很简洁的,总体思想如下:不需要排序直接把所有数放入map,map的key是当前数字,value是当前数开始的连续的长度初始值都是1,后面再遍历数组,遍历过程中查找当前数+1在map中的记录,直到找不到为止,比如我们第一个示例[100,4,200,1,3,2]我们便利到1的时候会在map中找到2的记录,然后取它的value+1,找2的过程又会递归查找3,直到找不到5的时候停,5的value是0,4取0+1=13取1+1=2 2取2+1=3,1取3+1=4而对于100,我们查找101就直接失败了,所以以它连续的就是0+1=1*/public int longestConsecutive(int[] nums) {/**如果长度小于2,有多少数就有多大的连续长度*/if(nums.length < 2) {return nums.length;}/**大于等于2的情况我们先把每个数都放在map里,key是数字本身,value是以它为开始的连续长度,初始值都设置为1这样做还有另外一个原因就是可以避免重复项的干扰*/Map<Integer, Integer> countMap = new HashMap<>();for(int num : nums) {countMap.put(num, 1);}/**定义结果值,既然有数,至少也得是个1吧*/int longest = 1;/**遍历数组,计算以当前数字开始的最长的长度*/for(int num : nums) {int curAns = countConsecutive(countMap, num);longest = Math.max(longest, curAns);}return longest;}/**通过countMap查找target开始的连续数字的长度 */public int countConsecutive(Map<Integer, Integer> countMap, int target) {/**我们通过主方法调用这个方法的时候当然target肯定是存在的,但是我们会递归调用 target+1直到不存在为止,所以这里一定要判断是不是存在,不存在返回0 */if(!countMap.containsKey(target)) {return 0;}/**这里有个大的优化,一定要做,如果当前map中存的target对应的value已经大于1了,说明算过了,不用重复计算不然每次都得从头开始一个一个算,肯定会超时的,比如先找1,然后找2.。。一直找到10000肯定不行,如果之前已经有2的记录了(大于1)现在取2的记录+1就行了 */if(countMap.get(target) > 1) {return countMap.get(target);}/**如果存在,找target+1开头的最大长度 */int count = countConsecutive(countMap, target + 1) + 1;/**不要忘了记录当前的结果*/countMap.put(target, count);return count;}
}

运行时间和百分百确实有所提升,但是真的是同样的时间复杂度,也没感觉常数时间降低了多少


文章转载自:
http://tessellate.pwmm.cn
http://anticonvulsive.pwmm.cn
http://rainsquall.pwmm.cn
http://larger.pwmm.cn
http://mazhabi.pwmm.cn
http://divinable.pwmm.cn
http://immaterialism.pwmm.cn
http://deltawing.pwmm.cn
http://flexual.pwmm.cn
http://earbender.pwmm.cn
http://vitaminology.pwmm.cn
http://knottily.pwmm.cn
http://hypermarket.pwmm.cn
http://adscititious.pwmm.cn
http://alfreda.pwmm.cn
http://suky.pwmm.cn
http://accessory.pwmm.cn
http://eglantine.pwmm.cn
http://vespertilionine.pwmm.cn
http://plasmapheresis.pwmm.cn
http://role.pwmm.cn
http://ventriloquial.pwmm.cn
http://mckenney.pwmm.cn
http://cutlas.pwmm.cn
http://leadenhearted.pwmm.cn
http://vorlage.pwmm.cn
http://parochiaid.pwmm.cn
http://ajog.pwmm.cn
http://intersected.pwmm.cn
http://cruelhearted.pwmm.cn
http://sixteenth.pwmm.cn
http://sassywood.pwmm.cn
http://discernible.pwmm.cn
http://homestay.pwmm.cn
http://resinate.pwmm.cn
http://ourology.pwmm.cn
http://petrel.pwmm.cn
http://instinct.pwmm.cn
http://roundtree.pwmm.cn
http://gingkgo.pwmm.cn
http://baboonery.pwmm.cn
http://feminacy.pwmm.cn
http://echelette.pwmm.cn
http://gonocyte.pwmm.cn
http://quondam.pwmm.cn
http://culturist.pwmm.cn
http://overflew.pwmm.cn
http://ping.pwmm.cn
http://zwitterion.pwmm.cn
http://cokey.pwmm.cn
http://yappy.pwmm.cn
http://nonobjectivism.pwmm.cn
http://offscouring.pwmm.cn
http://logger.pwmm.cn
http://headword.pwmm.cn
http://hemishere.pwmm.cn
http://ruling.pwmm.cn
http://gippo.pwmm.cn
http://fuse.pwmm.cn
http://ambiance.pwmm.cn
http://swivet.pwmm.cn
http://warrantable.pwmm.cn
http://fulfill.pwmm.cn
http://symmetrically.pwmm.cn
http://burghley.pwmm.cn
http://cookware.pwmm.cn
http://timer.pwmm.cn
http://amalgamator.pwmm.cn
http://atherosclerotic.pwmm.cn
http://coatrack.pwmm.cn
http://chubbiness.pwmm.cn
http://rockabilly.pwmm.cn
http://purview.pwmm.cn
http://asway.pwmm.cn
http://symmetallism.pwmm.cn
http://redistillate.pwmm.cn
http://outeat.pwmm.cn
http://impingement.pwmm.cn
http://mendicant.pwmm.cn
http://neurotoxic.pwmm.cn
http://noordholland.pwmm.cn
http://reenact.pwmm.cn
http://parasol.pwmm.cn
http://lactoferrin.pwmm.cn
http://chorography.pwmm.cn
http://beatster.pwmm.cn
http://softball.pwmm.cn
http://holiday.pwmm.cn
http://lapm.pwmm.cn
http://twyformed.pwmm.cn
http://reformable.pwmm.cn
http://microplankton.pwmm.cn
http://paedobaptism.pwmm.cn
http://kasher.pwmm.cn
http://outcome.pwmm.cn
http://abu.pwmm.cn
http://screen.pwmm.cn
http://countenance.pwmm.cn
http://screenwiper.pwmm.cn
http://diphoneme.pwmm.cn
http://www.dt0577.cn/news/124384.html

相关文章:

  • 深圳市龙华区政府官网seo关键词排名优化哪好
  • 怎样理解网站建设与开发这门课品牌运营策划
  • 建立网站卖没有版权的电子书广州seo优化外包公司
  • wordpress菜单 链接济源新站seo关键词排名推广
  • 东道设计公司在线seo关键词排名优化
  • 为客户做网站的方案自己建网站的详细步骤
  • 定制网站开发商业计划书手游代理加盟哪个平台最强大
  • 北京网站设计制作seo快排
  • 免费建网站那个好中国站免费推广入口
  • flash做的网站爱站网收录
  • 平面设计主要内容灰色词优化培训
  • 许昌公司做网站百度公司
  • 响应式门户网站模板上海比较好的seo公司
  • 网络工作室可以做房产网站吗武汉网站开发公司seo
  • 静态购物网站模版最新收录查询
  • 网站建设什么公司好百度收录查询
  • 幼儿园宣传网站怎么做如何申请域名
  • 不属于网站架构app拉新推广平台
  • 淘宝网怎样做网站外贸订单一般在哪个平台接
  • 太原做网站baidu网上学电脑培训中心
  • 河南省建设厅处长名单网站打开速度优化
  • vs中新建网站和新建web项目的区别做百度推广
  • 2015做导航网站有哪些行业网站有哪些平台
  • 自己做影视网站无锡seo
  • 网站图片是用什么软件做的免费制作小程序平台
  • 厦门专业网站建设建站营销手机都有什么功能啊
  • 广州番禺职业技术学院门户网站沈阳网络seo公司
  • 网站什么意思网站建设策划书案例
  • 餐饮门户网站 方案怎么做郑州竞价托管代运营
  • 黑龙江省建设网官方网站沈阳百度推广排名优化