Search for a Range

Given a sorted array of integers, find the starting and ending position of a given target value.

Your algorithm‘s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

很早之前就做的这道题,然而不明就理,这道题其实思路很简单,找到该元素出现的第一个位置和最后一个位置,两次二分。但是这两次在mid元素等于target时的操作不一样,寻找左元素时,则end = mid。右边界时start = end。代码如下:

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        if not nums:
            return [-1,-1]
        l = 0
        r = len(nums)-1
        while l+1 < r: #find left
            mid = l + (r-l)/2
            if nums[mid] == target:
                r = mid
            elif nums[mid] > target:
                r = mid
            else:
                l = mid
        if nums[l]==target:
            left = l
        elif nums[r]==target:
            left = r
        else:
            left = -1
        l = 0
        r = len(nums)-1
        while l+1 < r: #find the right
            mid = l+(r-l)/2
            if nums[mid] == target:
                l = mid
            elif nums[mid] > target:
                r = mid
            else:
                l = mid
        if nums[r]==target:
            right = r
        elif nums[l] == target:
            right = l
        else:
            right = -1
        return [left,right]
时间: 2024-10-30 10:31:23

Search for a Range的相关文章

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(l

【LeetCode】Search for a Range

Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For ex

leetcode 二分查找 Search for a Range

Search for a Range Total Accepted: 21480 Total Submissions: 78454My Submissions Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If

[LeetCode] Search for a Range(二分法)

Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [5, 7,

[LeetCode] 034. Search for a Range (Medium) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search for a Range (Medium) 链接: 题目:https://leetcode.com/problems/search-for-a-range/ 代码(github):https://github.com/illuz/leetcode 题意: 在有序数组中找到一个数的范围.(由于数

[leetcode]Search for a Range @ Python

原题地址:https://oj.leetcode.com/problems/search-for-a-range/ 题意: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not

Search for a Range问题

Search for a Range问题 leetcode java 二分查找 1. 问题描述 Given an array of integers sorted in ascending order, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n).If the target is

LeetCode: Search for a Range [033]

[题目] Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For example, Given [

[LeetCode][JavaScript]Search for a Range

Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. For ex

LeetCode 034 Search for a Range

题目要求:Search for a Range Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. F