@@ -0,0 +1,1777 @@
+/**%0A *%09Given a sorted array of integers, find the starting and ending position of a given target value.%0A *%09Your algorithm's runtime complexity must be in the order of O(log n).%0A *%09If the target is not found in the array, return %5B-1, -1%5D.%0A *%0A *%09For example,%0A *%09Given %5B5, 7, 7, 8, 8, 10%5D and target value 8,%0A *%09return %5B3, 4%5D.%0A *%0A *%09Author: matao%0A */%0Apublic class SearchForARange %7B%0A%09public static int%5B%5D searchRange(int%5B%5D nums, int target) %7B%0A        int start = 0, end = nums.length-1;%0A        int%5B%5D bound = new int%5B2%5D;%0A        //find 1st pos equals target%0A        while(start + 1 %3C end) %7B%0A            int mid = start + (end - start) / 2;%0A            if(nums%5Bmid%5D == target)%0A                end = mid;%0A            else if(nums%5Bmid%5D %3E target)%0A                end = mid;%0A            else%0A                start = mid;%0A        %7D%0A        if(nums%5Bstart%5D == target)%0A            bound%5B0%5D = start;%0A        else if(nums%5Bend%5D == target)%0A            bound%5B0%5D = end;%0A        else%0A            bound%5B0%5D = -1;%0A        %0A        //find last pos equals target%0A        start = 0;%0A        end = nums.length-1;%0A        while(start + 1 %3C end) %7B%0A            int mid = start + (end - start) / 2;%0A            if(nums%5Bmid%5D == target)%0A                start = mid;%0A            else if(nums%5Bmid%5D %3E target)%0A                end = mid;%0A            else%0A                start = mid;%0A        %7D%0A        if(nums%5Bend%5D == target)%0A            bound%5B1%5D = end;%0A        else if(nums%5Bstart%5D == target)%0A            bound%5B1%5D = start;%0A        else%0A            bound%5B1%5D = -1;%0A        %0A        return bound;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A    %09int%5B%5D nums = %7B5, 7, 7, 8, 8, 10%7D;%0A    %09int%5B%5D bound = searchRange(nums, 8);%0A    %09for(int i = 0; i %3C bound.length; i++)%0A    %09%09System.out.print(bound%5Bi%5D + %22 %22);%0A    %7D%0A%7D

add search for a range solution