my java solution, one binary search


  • 0
    H

    but I don't know whether my time complexity is O(log n)

    class Solution {
        public int[] searchRange(int[] nums, int target) {
            int l = 0, r = nums.length - 1;
            int[] res = {-1, -1};
            while(l <= r){
                int mid = (l + r)/2;
                if(nums[mid] == target){
                    int i = mid , j = mid;
                    while(i >= 1 && nums[i - 1] == target) i--;
                    res[0] = i;
                    while(j <= nums.length - 2 && nums[j + 1] == target) j++;
                    res[1] = j;
                    return res;
                }
                else if(nums[mid] < target) l = mid + 1;
                else r = mid - 1;
            }
            return res;
        }
    }
    

  • 0
    J

    O(logn + k). n is the size of input, and k is the number of target in array.


  • 0
    H
    This post is deleted!

  • 0
    H

    @junm5 thank you for your answer


Log in to reply
 

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