Topic one: the number of times the number that appears in the sort array
method: using a binary search to find that number, and then expand the statistics about
Method Two: respectively binary search to find the first and last k K, and then subtract
static int findk(int a[],int k) { if(a==null||a.length<1) return 0; int be=0; int end=a.length-1; int i1=-1; int inter; while(be<=end) { inter=(be+end)>>1; if(a[inter]<k) be=inter+1; else if(a[inter]>k) end=inter-1; else { if(inter==0||a[inter-1]<k) { i1=inter; break; } else end=inter-1; } } if(i1==-1)return 0; be=i1; end=a.length-1; int i2=-1; while(be<=end) { inter=(be+end)>>1; if(a[inter]<k) be=inter+1; else if(a[inter]>k) end=inter-1; else { if(inter==a.length-1||a[inter+1]>k) { i2=inter; break; } else be=inter+1; } } if(i2>=i1) return i2-i1+1; return 0; }
Title Two: the 0-n-1 deletion element
a length in ascending order in the array numbers n-1 unique numerical ranges within 0-n-1 of the n digital one and only one digit from the array, find the digital
method: direct use of binary search, if the values and subscript are equal, then the digits to the right if the value is greater than the index, and it's on a value equal to the index returns under the current standard, otherwise it on the left
1 static int GetmissNumber(int a[]) { 2 if(a==null||a.length<1) return 0; 3 int be=0; 4 int end=a.length-1; 5 int <u>i1</u>=-1; 6 int inter; 7 while(be<=end) { 8 inter=(be+end)>>1; 9 if(a[inter]==inter) 10 be=inter+1; 11 else { 12 if(inter==0||(a[inter]>inter&&a[inter-1]==inter-1)) 13 return inter; 14 end=inter-1; 15 } 16 } 17 return -1; 18 }
Title III: Find the same element value index
method: binary search; if the index is greater than the value that the right, if the index is less than the value at the left side, can not find -1