34. Search for a Range
public class Solution { public int[] searchRange(int[] nums, int target) { return new int[]{binarySearch(nums,target,true),binarySearch(nums,target,false)}; } private int binarySearch(int[] nums, int target, boolean mode) { int l=0; int r=nums.length-1; while(l<r) { int m=l+(r-l)/2; if(nums[m]==target) { if(mode==true) r=m; else { if(nums[m+1]==target) l=m+1; else return m; } } else if(nums[m]<target) l=m+1; else r=m; } return l<nums.length&&nums[l]==target?l:-1; } }