Share java code


  • 0
    G
    public class Solution {
        public int[] searchRange(int[] nums, int target) {
           
            int lo=0;
            int high=nums.length-1;
            int[] ans={-1,-1};
            
            if(nums.length<1) return ans; 
            
            while(high>lo){
                
                int mid=lo+(high-lo)/2;
                if(nums[mid]<target){
                    lo=mid+1;
                    
                }else{
                    high=mid;
                }
            }
         
           
           if(nums[lo]!=target){return ans;}
           else{
               ans[0]=lo;          
            while(++lo<=nums.length-1&&nums[lo]==target){}
                   ans[1]=lo-1;
           }
             return ans;
        }
    }

  • 0

    Your solution is O(n) if every element of the array equals target!


Log in to reply
 

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