标签:
public class Solution { public int[] searchRange(int[] A, int target) { int[] res = {-1, -1}; if(A==null || A.length<1) return res; int ll = 0, lr = A.length-1, rl=0,rr = A.length-1; while(ll<=lr){ int m = (ll+lr)/2; if(A[m]<target){ ll=m+1; }else lr= m-1; } while(rl<=rr){ int m = (rl+rr)/2; if(A[m]<=target){// 如果是< 就错了,因为rr的位置和ll一样,必然不可以是等于的情况 rl=m+1; }else rr= m-1; } if(ll<=rr){ res[0] = ll; res[1] = rr; } return res; } }
二分法和夹逼法
ref
http://blog.csdn.net/linhuanmars/article/details/20593391
标签:
原文地址:http://www.cnblogs.com/jiajiaxingxing/p/4423893.html