LeetCode | 0034. 在排序数组中查找元素的第一个和最后一个位置【Python】

LeetCode 0034. Find First and Last Position of Element in Sorted Array在排序数组中查找元素的第一个和最后一个位置【Medium】【Python】【二分】

Problem

LeetCode

Given an array of integers nums 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 not found in the array, return [-1, -1].

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]

问题

力扣

给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

你的算法时间复杂度必须是 O(log n) 级别。

如果数组中不存在目标值,返回 [-1, -1]

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: [3,4]

示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: [-1,-1]

思路

二分查找

两次二分查找。
1. 查找 left,所以 nums[mid] < target 时,才移动 left 指针
2. 查找 right,所以 nums[mid] <= target 时,才移动 left 指针
3. lower_bound 返回的是开始的第一个满足条件的位置,而 upper_bound 返回的是第一个不满足条件的位置。所以,当两个相等的时候代表没有找到,如果找到了的话,需要返回的是 [left, right - 1]。

时间复杂度: O(logn)
空间复杂度: O(1)

Python代码

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        # solution one: binary search
        left = self.lowwer_bound(nums, target)
        right = self.higher_bound(nums, target)
        if left == right:
            return [-1, -1]
        return [left, right - 1]

    def lowwer_bound(self, nums, target):
        # find in range [left, right)
        left, right = 0, len(nums)
        while left < right:
            mid = int((left + right) / 2)
            if nums[mid] < target:  # <
                left = mid + 1
            else:
                right = mid
        return left

    def higher_bound(self, nums, target):
        # find in range [left, right)
        left, right = 0, len(nums)
        while left < right:
            mid = int((left + right) / 2)
            if nums[mid] <= target:  # <=
                left = mid + 1
            else:
                right = mid
        return left

        # # solution two: bisect
        # left = bisect.bisect_left(nums, target)
        # right = bisect.bisect_right(nums, target)
        # if left == right:
        #     return [-1, -1]
        # return [left, right - 1]

代码地址

GitHub链接

LeetCode个人题解 文章被收录于专栏

LeetCode个人题解,目前主要是 Python3 题解。

全部评论

相关推荐

10-17 09:06
门头沟学院 Java
8527睿:有些地方感觉不太契合实际啊。简单看看第二个项目那里。 比如canal流式读取数据库日志进行缓存同步那里。可不可以加个消息中间件来确保SQL语句的削峰填谷。一般都是canal+消息中间件 双层鉴权登录那里,描述有点模糊,登录是鉴权的前提唉,后面功能都在说是登录,鉴权没有啊
点赞 评论 收藏
分享
影04714:把图书管理系统那个项目经验内容适当的减少掉,然后改成据为己有不要说团队项目,因为图书管理系统这类常见的谁来了都能独立写出来,提问能圆过来即可
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务