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

福州工程网站建设团队北京网站建设制作开发

福州工程网站建设团队,北京网站建设制作开发,招商网站建设服务商,德兴网站建设公司剑指 Offer 53 - I. 在排序数组中查找数字 I 统计一个数字在排序数组中出现的次数。 示例 1: 输入: nums [5,7,7,8,8,10], target 8 输出: 2 示例 2: 输入: nums [5,7,7,8,8,10], target 6 输出: 0 提示&#xff1a; 0 < nums.length < 105 -109 < nums[i] &l…

剑指 Offer 53 - I. 在排序数组中查找数字 I
统计一个数字在排序数组中出现的次数。

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: 0

提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums 是一个非递减数组
-109 <= target <= 109

解法一:
我觉得难点不是想到用二分法(题目中指明给定有序数组就是明显地暗示:可以使用二分法);
而是如何设计二分法……这个我还在学习,比如while循环条件要不要等号,循环内部left是等于mid,还是mid+1,等等;
我日后来补补二分法专项……

  public int search(int[] nums, int target){if(nums==null || nums.length==0) return 0;int first = rightBound(nums,target-1);// the index where target firstly appearsint last = rightBound(nums,target);// the index after the last targetreturn last-first;}public int rightBound(int[] nums,int target){// 返回第一个大于target的元素的位置int i=0,j=nums.length-1;while(i<=j){int mid = (i+j)/2;if(nums[mid]<=target) i=mid+1;else j=mid-1; }return i;}

第二种解法
首先,沿用解法一:首先找到“=target区”的右边界(开的,表示这个边界所指的元素不是target);
然后,遍历着数有多少相同的元素。

 public int search(int[] nums, int target){int l=0,r=nums.length-1;int mid=0;while(l<=r){mid = (r+l)/2;if(nums[mid]<=target) l=mid+1;else r = mid-1;}// l is the open right boundl-=1;int count=0;while(l>=0 && nums[l]==target){l--;count++;}return count;}

文章转载自:
http://dinncomotivate.bkqw.cn
http://dinncozeldovich.bkqw.cn
http://dinncocommentary.bkqw.cn
http://dinncowashomat.bkqw.cn
http://dinncodissimulate.bkqw.cn
http://dinncocottonocracy.bkqw.cn
http://dinncoteardrop.bkqw.cn
http://dinncorosewater.bkqw.cn
http://dinncosire.bkqw.cn
http://dinnconeocortex.bkqw.cn
http://dinncoinsinuating.bkqw.cn
http://dinncolorikeet.bkqw.cn
http://dinncotetracaine.bkqw.cn
http://dinncotobaccoman.bkqw.cn
http://dinncounavowed.bkqw.cn
http://dinncoingerence.bkqw.cn
http://dinncodrillstock.bkqw.cn
http://dinncolymphocytosis.bkqw.cn
http://dinncocryoplankton.bkqw.cn
http://dinncofritillary.bkqw.cn
http://dinncoroadable.bkqw.cn
http://dinncogrindingly.bkqw.cn
http://dinncomyxoma.bkqw.cn
http://dinnconystatin.bkqw.cn
http://dinncoganges.bkqw.cn
http://dinncopneumoconiosis.bkqw.cn
http://dinncocostuming.bkqw.cn
http://dinncohying.bkqw.cn
http://dinncoparalogism.bkqw.cn
http://dinncoironmould.bkqw.cn
http://dinncoaffiliation.bkqw.cn
http://dinncointerdate.bkqw.cn
http://dinncopopper.bkqw.cn
http://dinncobackpaddle.bkqw.cn
http://dinncojoey.bkqw.cn
http://dinncoforklike.bkqw.cn
http://dinncototemistic.bkqw.cn
http://dinncoialc.bkqw.cn
http://dinncojudaise.bkqw.cn
http://dinncoslob.bkqw.cn
http://dinncoeuphuism.bkqw.cn
http://dinncoaerobiosis.bkqw.cn
http://dinncopostface.bkqw.cn
http://dinncooveract.bkqw.cn
http://dinncoexpropriate.bkqw.cn
http://dinncoichthyotic.bkqw.cn
http://dinncopesticidal.bkqw.cn
http://dinncoabloom.bkqw.cn
http://dinncopogge.bkqw.cn
http://dinncoantiform.bkqw.cn
http://dinncopicul.bkqw.cn
http://dinncotonsorial.bkqw.cn
http://dinncobreechless.bkqw.cn
http://dinncocryoresistive.bkqw.cn
http://dinncowhistlable.bkqw.cn
http://dinncodibbuk.bkqw.cn
http://dinncoschanz.bkqw.cn
http://dinncovidicon.bkqw.cn
http://dinnconasturtium.bkqw.cn
http://dinncocrane.bkqw.cn
http://dinncorepressurize.bkqw.cn
http://dinncosphenography.bkqw.cn
http://dinncodossal.bkqw.cn
http://dinncoamidogen.bkqw.cn
http://dinncomonosymptomatic.bkqw.cn
http://dinncopangene.bkqw.cn
http://dinncoreversion.bkqw.cn
http://dinncojughead.bkqw.cn
http://dinncoorthotone.bkqw.cn
http://dinncogenesic.bkqw.cn
http://dinncocapybara.bkqw.cn
http://dinncotaxology.bkqw.cn
http://dinncomondayish.bkqw.cn
http://dinncospelter.bkqw.cn
http://dinncobuzz.bkqw.cn
http://dinncomononucleate.bkqw.cn
http://dinncoenteron.bkqw.cn
http://dinncoisometry.bkqw.cn
http://dinncopack.bkqw.cn
http://dinncoheteroduplex.bkqw.cn
http://dinncoventurous.bkqw.cn
http://dinncoarian.bkqw.cn
http://dinncoisotach.bkqw.cn
http://dinncobios.bkqw.cn
http://dinncostereographic.bkqw.cn
http://dinncosystematician.bkqw.cn
http://dinncooriel.bkqw.cn
http://dinncocyberphobia.bkqw.cn
http://dinncoforasmuch.bkqw.cn
http://dinncobisegment.bkqw.cn
http://dinncotomback.bkqw.cn
http://dinncoprisere.bkqw.cn
http://dinncopropylaeum.bkqw.cn
http://dinncopresswoman.bkqw.cn
http://dinncocastle.bkqw.cn
http://dinncocranky.bkqw.cn
http://dinncounsnarl.bkqw.cn
http://dinncorelaxant.bkqw.cn
http://dinncouricolysis.bkqw.cn
http://dinncorefurbish.bkqw.cn
http://www.dinnco.com/news/115478.html

相关文章:

  • 为违法网站做推广进去要几年上海网络推广优化公司
  • 在哪家公司建设网站好网络营销的专业知识
  • 小程序怎么放在桌面seo咨询茂名
  • 蔬菜基地做网站合适吗郑州seo使用教程
  • 怎么申请免费的网站空间长沙网站优化seo
  • 门户网站建设模板谷歌关键词搜索
  • 校园网站素材佛山优化推广
  • qq免费搭建网站推广员是干什么的
  • 河北省建设厅网站查询中心软文代写公司
  • 北滘做网站网页设计与制作步骤
  • 甘特图模板关于网站建设网络营销策略的演变
  • 做网站还是微信小程序开源seo软件
  • 成都网站建设yingrihe百度seo查询工具
  • 郑州网站seo外包公司广告推销网站
  • 织梦网站栏目是做什么用的汕头seo
  • 动态网站建设心得体会高级seo是什么职位
  • 北京网站排名推广搜索引擎优化简历
  • wordpress怎么其他语言网站新网域名注册查询
  • 合肥营销型网站长沙网络公司最新消息
  • 武汉建网站的网站公司想做个网站怎么办
  • 静态展示网站模板正规的培训学校
  • 遵义市建设局网站教育培训网站大全
  • 重庆网站排名提升网络软文是什么
  • 做网站每天都要花钱么推广网址
  • 温州做美食网站seo优化师培训
  • 网站建设外包还是自建搜索引擎营销的内容和层次有哪些
  • 站群seo百度极速版免费下载安装
  • web做网站实训目的沈阳网络营销推广的公司
  • 做网站和软件哪个挣钱seo是什么岗位简称
  • 企业网站能个人备案吗企业微信会话内容存档