034 Search for a Range

034 Search for a Range

二分搜索, 搜左右部分稍微有些不同 需要注意

class Solution:
    # @param {integer[]} nums
    # @param {integer} target
    # @return {integer[]}
    def searchRange(self, nums, target):
        left = self.findIndex(nums, target, "left", 0, len(nums)-1)
        if left == -1:
            return [-1,-1]
        right = self.findIndex(nums, target, "right", left, len(nums)-1)
        return [left, right]

    def findIndex(self, nums, target, model, left, right):
        if left > right:
                return -1
        if left == right:
            if nums[left] == target:
                return left
            else:
                return -1
        if model == "left":
            mid = (left + right) / 2
        else:
            mid = (left + right + 1) / 2
        if nums[mid] > target:
            right = mid - 1
        elif nums[mid] < target:
            left = mid + 1
        else:
            if model == "left":
                right = mid
            else:
                left = mid
        return self.findIndex(nums, target, model, left, right)
时间: 2024-10-18 20:50:29

034 Search for a Range的相关文章

[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 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

Java for LeetCode 034 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 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]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