A easy understand java solution


  • 0
    H
    public class Solution {
        public int[] searchRange(int[] nums, int target) {
             int length=nums.length;
    				
    		        int start=-1,end=-1;
    		        int low=0,high=length-1;
    		        while(low<=high){
    		        	int mid=(low+high)/2;
    		        	if(nums[mid]==target){
    		        		start=mid;
    		        		 end=start;
    		        	while(start-1>=0&&nums[start-1]==target){
    		        		start=start-1;
    		        	}
    		        	while(end+1<=length-1&&nums[end+1]==target){
    		        		end=end+1;
    		        	}
    				
    		        	  return new int[] res={start,end};
    		        	}
    		        	if(nums[mid]<target)
    		        		low=mid+1;
    		        	else high=mid-1;
    		        }
    		          return new int[] res={-1,-1};	      
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.