• Leetcode题库——34.在排序数组中国查找元素的第一个和最后一个位置



    @author: ZZQ
    @software: PyCharm
    @file: searchRange.py
    @time: 2018/11/12 19:19
    要求:给定一个按照升序排列的整数数组 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. 二分查找,先找到该元素的位置,然后向前向后搜索其起止位置(好像会到O(n))
    2. 分两次搜索,二分查找,先找到起始位置,再找到停止位置。

    class Solution():
        def __init__(self):
            pass
    
        def searchRange(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            nums_len = len(nums)
            start_index = -1
            end_index = -1
            if nums_len == 0:
                return [start_index, end_index]
            elif nums_len == 1:
                if nums[0] == target:
                    return [0, 0]
                else:
                    return [start_index, end_index]
            else:
                left = 0
                right = nums_len-1
                while left <= right:
                    middle = (left + right) / 2
                    if nums[middle] == target:
                        start_index = middle
                        while nums[middle] == target:
                            middle += 1
                            if middle == nums_len:
                                end_index = middle - 1
                                break
                        if end_index == -1:
                            end_index = middle - 1
                        while nums[start_index-1] == target:
                            start_index -= 1
                            if start_index < 0:
                                start_index += 1
                                break
                        return [start_index, end_index]
                    elif nums[middle] < target:
                        left = middle + 1
                    else:
                        right = middle - 1
                return [start_index, end_index]
    
    
        def searchRange2(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            nums_len = len(nums)
            start_index = -1
            end_index = -1
            if nums_len == 0:
                return [start_index, end_index]
            elif nums_len == 1:
                if nums[0] == target:
                    return [0, 0]
                else:
                    return [start_index, end_index]
            else:
                left = 0
                right = nums_len-1
                min = nums_len-1
                max = 0
                while left <= right:
                    middle = (left + right) / 2
                    if nums[middle] == target:
                        if middle < min:
                            min = middle
                        if middle > max:
                            max = middle
                        right -= 1
                    elif nums[middle] < target:
                        left = middle + 1
                    else:
                        right = middle - 1
                left = 0
                right = nums_len - 1
                while left <= right:
                    middle = (left + right) / 2
                    if nums[middle] == target:
                        if middle < min:
                            min = middle
                        if middle > max:
                            max = middle
                        left += 1
                    elif nums[middle] < target:
                        left = middle + 1
                    else:
                        right = middle - 1
                if min > max:
                    return [-1, -1]
                else:
                    return [min, max]
    
  • 相关阅读:
    ANDROID笔记:shape的简单使用
    ANDROID笔记:根据长宽实现图片压缩
    ANDROID笔记:PopupMenu的用法
    ANDROID笔记:AdapterContextMenuInfo在ListView中的用法
    ANDROID笔记:利用XML布局文件生成Menu
    ANDROID笔记:ContextMenu的用法
    ANDROID笔记:JSON和Adapter的使用
    ANDROID笔记:Dialog的几种用法
    ANDROID笔记:AdapterViewFlipper和ViewFlipper的简单使用
    #2020征文-开发板# 用鸿蒙开发AI应用(一)硬件篇
  • 原文地址:https://www.cnblogs.com/zzq-123456/p/9965451.html
Copyright © 2020-2023  润新知