zoukankan      html  css  js  c++  java
  • 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]
    
  • 相关阅读:
    Linux基础(Ubuntu16.04):安装vim及配置
    Ubuntu16.04 安装ROS及其IDE
    python格式转换的记录
    python爬虫selenium相关
    【LAMP】搭建Web网站过程中的记录【Ubuntu18.04+Apache2.4+PHP7.2+MySQL5.7】
    【疯狂挖坑】linux服务器尝试中的问题(nohup等)
    逻辑回归与全连接神经网络联系的简单理解记录
    Unity3d开发中遇到的问题记录
    python中numpy库的一些使用
    Deep Learning论文翻译(Nature Deep Review)
  • 原文地址:https://www.cnblogs.com/zzq-123456/p/9965451.html
Copyright © 2011-2022 走看看