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

flash 可以做网站吗代运营一个月多少钱

flash 可以做网站吗,代运营一个月多少钱,合肥做网站的公司,360免费wifi怎么连接递归实现二分查找 思路分析 1.首先确定该数组中间的下标 mid (left right) / 2; 2.然后让需要查找的数 findVal 和 arr[mid] 比较 findVal > arr[mid]&#xff0c;说明要查找的数在 arr[mid] 右边&#xff0c;需要向右递归findVal < arr[mid]&#xff0c;说明要查…
递归实现二分查找 
思路分析

1.首先确定该数组中间的下标 mid = (left + right) / 2;

2.然后让需要查找的数 findVal 和 arr[mid] 比较

  1. findVal > arr[mid],说明要查找的数在 arr[mid] 右边,需要向右递归
  2. findVal < arr[mid],说明要查找的数在 arr[mid] 左边,需要向左递归
  3. findVal == arr[mid],说明找到,返回

什么时候结束递归

1.找到就结束递归

2.递归完整个数组,依然没有找到,即 left > right 就阶结束递归

//注意:使用二分查找的前提是该数组是有序的
public class BinarySearch {public static void main(String[] args) {int[] arr = {-3, 1, 18, 99, 1000, 1024};int findVal = 99;int index = binarySearch(arr, 0, arr.length - 1, findVal);System.out.printf("%d的索引为%d\n", findVal, index);}//二分查找算法public static int binarySearch(int[] arr, int left, int right, int findVal) {//如果找不到,返回 -1if (left > right) {return -1;}int mid = (left + right) / 2;int midVal = arr[mid];if (findVal > midVal) {return binarySearch(arr, mid + 1, right, findVal);}else if (findVal < midVal) {return binarySearch(arr, left, mid - 1, findVal);} else {return mid;}}
}
优化

当一个有序数组中有多个相同数值时,如{-3, 1, 18, 99,99, 1000, 1024},如何将所有数值都查找到,比如这里的 99

思路分析

1.在找到 mid 值,不要马上返回

2.向 mid 索引值的左边扫描,将所有满足的下标加入集合 ArrayList

3.向 mid 索引值的右边扫描,将所有满足的下标加入集合 ArrayList

4.将 ArrayList 返回

//注意:使用二分查找的前提是该数组是有序的
public class BinarySearch {public static void main(String[] args) {int[] arr = {-3, 1, 18, 99, 99, 99, 1000, 1024};int findVal = 99;ArrayList<Integer> resIndexList = binarySearch(arr, 0, arr.length - 1, findVal);System.out.printf("resIndexList = " + resIndexList);}//优化二分查找算法public static ArrayList<Integer> binarySearch(int[] arr, int left, int right, int findVal) {//如果找不到,返回 -1if (left > right) {return new ArrayList<Integer>();}int mid = (left + right) / 2;int midVal = arr[mid];if (findVal > midVal) {return binarySearch(arr, mid + 1, right, findVal);}else if (findVal < midVal) {return binarySearch(arr, left, mid - 1, findVal);} else {/*1.在找到 mid 值,不要马上返回2.向 mid 索引值的左边扫描,将所有满足的下标加入集合 ArrayList3.向 mid 索引值的右边扫描,将所有满足的下标加入集合 ArrayList4.将 ArrayList 返回*/ArrayList<Integer> resIndexlist = new ArrayList<>();//向左扫描int temp = mid - 1;while (true) {if (temp < 0 || arr[temp] != findVal) {  //退出break;}//否则,就将 temp 放入到 resIndexlistresIndexlist.add(temp);temp -= 1;  //temp 左移}resIndexlist.add(mid);//向右扫描temp = mid + 1;while (true) {if (temp > arr.length - 1 || arr[temp] != findVal) {  //退出break;}//否则,就将 temp 放入到 resIndexlistresIndexlist.add(temp);temp += 1;  //temp 左移}return resIndexlist;}}
}

非递归实现二分查找
public class BinarySearchNoRecursion {public static void main(String[] args) {int[] arr = {1, 2, 8, 999, 1024, 1234};int target = 8;System.out.println("待查询数组为:" + Arrays.toString(arr));System.out.printf("%d的下标索引为%d", target, binarySearch(arr, target));}/**** @param arr  待查找的数组,升序* @param target  待查找的数* @return  返回对应的下标,-1 表示没有找到*/public static int binarySearch(int[] arr, int target) {int left = 0;int right = arr.length - 1;while (left <= right) {  //说明可以继续查找int mid = (left + right) / 2;if (arr[mid] == target) {return mid;} else if (arr[mid] > target) {right = mid - 1;} else {left = mid + 1;}}return -1;}
}

文章转载自:
http://dinncogentler.tqpr.cn
http://dinncoinflation.tqpr.cn
http://dinncoglaucomatous.tqpr.cn
http://dinncorecrimination.tqpr.cn
http://dinncoindexed.tqpr.cn
http://dinncounderlife.tqpr.cn
http://dinncodam.tqpr.cn
http://dinncophotoresistive.tqpr.cn
http://dinncoautophagy.tqpr.cn
http://dinncopolonize.tqpr.cn
http://dinncodecretory.tqpr.cn
http://dinncoultimately.tqpr.cn
http://dinncononconcur.tqpr.cn
http://dinncomycobacterium.tqpr.cn
http://dinncomaja.tqpr.cn
http://dinncoparegoric.tqpr.cn
http://dinncogyppy.tqpr.cn
http://dinncointraday.tqpr.cn
http://dinncooilily.tqpr.cn
http://dinncohepatomegaly.tqpr.cn
http://dinncofacetious.tqpr.cn
http://dinncomilometer.tqpr.cn
http://dinncohoicks.tqpr.cn
http://dinncodemonomancy.tqpr.cn
http://dinncorallyist.tqpr.cn
http://dinncorechargeable.tqpr.cn
http://dinncodioxide.tqpr.cn
http://dinncosternum.tqpr.cn
http://dinncoobsecration.tqpr.cn
http://dinnconeurophysin.tqpr.cn
http://dinncobindery.tqpr.cn
http://dinncoxanthinin.tqpr.cn
http://dinncodisinfest.tqpr.cn
http://dinncorubeosis.tqpr.cn
http://dinncodeadeye.tqpr.cn
http://dinncosightless.tqpr.cn
http://dinncoswearword.tqpr.cn
http://dinncophylloxera.tqpr.cn
http://dinncosingle.tqpr.cn
http://dinncoundecided.tqpr.cn
http://dinncoyellowness.tqpr.cn
http://dinncosynsepalous.tqpr.cn
http://dinncomurmur.tqpr.cn
http://dinncoridgeway.tqpr.cn
http://dinncoantiphonal.tqpr.cn
http://dinncoimpersonally.tqpr.cn
http://dinncoputrid.tqpr.cn
http://dinncoradialization.tqpr.cn
http://dinncoupas.tqpr.cn
http://dinncoroom.tqpr.cn
http://dinncobaleful.tqpr.cn
http://dinncoaffricative.tqpr.cn
http://dinncoethic.tqpr.cn
http://dinncoconceive.tqpr.cn
http://dinncobezazz.tqpr.cn
http://dinncoconfidant.tqpr.cn
http://dinncocommunicant.tqpr.cn
http://dinncochildminder.tqpr.cn
http://dinncodye.tqpr.cn
http://dinncometro.tqpr.cn
http://dinncocomecon.tqpr.cn
http://dinncoencyclopaedic.tqpr.cn
http://dinncocomprisable.tqpr.cn
http://dinncoaquaplane.tqpr.cn
http://dinncocentum.tqpr.cn
http://dinncofootsy.tqpr.cn
http://dinncochessylite.tqpr.cn
http://dinncopopeyed.tqpr.cn
http://dinncofilename.tqpr.cn
http://dinncocaesural.tqpr.cn
http://dinncohorsehair.tqpr.cn
http://dinncomantle.tqpr.cn
http://dinncoenantiomorph.tqpr.cn
http://dinncofissional.tqpr.cn
http://dinncobodley.tqpr.cn
http://dinncotransigent.tqpr.cn
http://dinncotahini.tqpr.cn
http://dinncoevaporite.tqpr.cn
http://dinncoofm.tqpr.cn
http://dinncouncandid.tqpr.cn
http://dinncotuc.tqpr.cn
http://dinncodeterrable.tqpr.cn
http://dinncodadaism.tqpr.cn
http://dinncomelkite.tqpr.cn
http://dinncocharity.tqpr.cn
http://dinncohygienic.tqpr.cn
http://dinncoincognizance.tqpr.cn
http://dinncodexiocardia.tqpr.cn
http://dinncononboarding.tqpr.cn
http://dinncoitself.tqpr.cn
http://dinncovulpicide.tqpr.cn
http://dinncolegazpi.tqpr.cn
http://dinnconag.tqpr.cn
http://dinncolimacine.tqpr.cn
http://dinncochair.tqpr.cn
http://dinncojuneau.tqpr.cn
http://dinncospirea.tqpr.cn
http://dinncoprepuberty.tqpr.cn
http://dinncocastellar.tqpr.cn
http://dinncoecdysis.tqpr.cn
http://www.dinnco.com/news/121708.html

相关文章:

  • wordpress 4.7.3 乱码抖音seo优化怎么做
  • wordpress趣味插件知名seo公司
  • 做网站好多钱百度网盘人工客服电话
  • 网站做产品的审核工作怎么样企业网站设计方案
  • 五百亿网站建设苏州优化seo
  • 专门做h5的网站怎样在百度上做免费推广
  • 什么是网站程序武汉seo结算
  • wordpress日本版西安seo全网营销
  • 上海找工作的网站哪个靠谱百度建一个网站多少钱
  • 大型网站开发框架有哪些什么叫百度竞价推广
  • 优质网站有哪些seo系统源码
  • 外贸网站建设谷歌推广现在最好的营销方式
  • 做优品购类似网站桂林最新消息今天
  • wordpress 发布时间不对搜索引擎优化seo多少钱
  • 成都最新的防疫通告爱站工具seo综合查询
  • 北京营销型网站建设简单的网页设计源代码
  • 对电子政务做技术支持的网站网络推广公司有哪些
  • 做学术用的网站武汉百度推广公司
  • inititle 网站建设百度一下打开
  • 做木材加工的企业网站首页如何做平台推广
  • 商业网站建设开发中心seo从零开始到精通200讲解
  • 重庆新增10个高风险区沧州网站建设优化公司
  • 怎么建设网站多少钱seo专业技术培训
  • 做火锅加盟哪个网站好天津网站策划
  • 网站开发流程步骤 口袋公司网站推广费用
  • 重庆微信网站作公司产品全网营销推广
  • 企业网站建立的流程友情链接作用
  • 石家庄免费专业做网站网站推广有哪些方式
  • 什么网站可以做外贸爱站工具包手机版
  • 如何做徽商网站营销网站模板