
首先循环法实现二分查找的模版给我背过。精髓在于把退出循环的条件放在了while()里。//二分查找法模版背过 //因为我们拥有整个数组的全局视野所以用循环就好了不写递归了 int findTarget(int[] nums, int target){ int left 0; int rightnums.length-1; //退出循环的条件放在while里 while(leftright){ int mid(leftright)/2; if(nums[mid]target) return mid; else if(targetnums[mid]) leftmid1; //不用去做left是否溢出的判断都在while()里做了 else rightmid-1; //不用做right是否溢出的判断都在while()里做了 } return -1; //没有 }二分查找的变种但只修改了if(nums[mid]tartget)这个分支标准的二分查找命中后直接return但是这里我们不return继续找。进行两次二分查找第一次找最大下标第二次找最小下标。class Solution { public int[] searchRange(int[] nums, int target) { //特殊情况 if(nums.length0) return new int[]{-1,-1}; int targetMaxIndex findTargetMaxIndex(nums, target); int targetMinIndex; if(targetMaxIndex-1){ return new int[]{-1,-1}; }else{ targetMinIndex findTargetMinIndex(nums,target); } return new int[] {targetMinIndex,targetMaxIndex}; } private int findTargetMaxIndex(int[] nums,int target){ //进入循环前的初始化 int left0; int rightnums.length-1; int maxIndex-1; while(leftright){ int mid(leftright)/2; //如果命中了不return假装没找到继续往右边找更右边的 if(nums[mid]target) { maxIndex Math.max(maxIndex, mid); left mid 1; //没命中target在nums[mid]右边继续往右边找(跟二分查找一样 }else if(targetnums[mid]){ leftmid1; //没命中target在nums[mid]左边继续往左边找(跟二分查找一样 }else{ rightmid-1; } } if(maxIndex!-1) return maxIndex; return -1; } private int findTargetMinIndex(int[] nums,int target){ //进入循环前的初始化 int left0; int rightnums.length-1; int minIndexnums.length; while(leftright){ int mid(leftright)/2; //如果命中了不return假装没找到继续往左边找更左边的 if(nums[mid]target){ minIndexMath.min(minIndex,mid); rightmid-1; //(跟二分查找一样 }else if(targetnums[mid]){ leftmid1; //(跟二分查找一样 }else{ rightmid-1; } } if(minIndex!nums.length) return minIndex; return -1; } }