[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array

  • 本题难度: Medium/Medium
  • Topic: Binary Search

    Description

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

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

Example

Example 1:

Input:

[]

9

Output:

[-1,-1]

Example 2:

Input:

[5, 7, 7, 8, 8, 10]

8

Output:

[3, 4]

Challenge

O(log n) time.

我的代码

class Solution:
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        l = 0
        r = len(nums) - 1
        m = int((l + r) / 2)
        r1 = -1
        r2 = -1
        while (l <= r):
            if nums[m] == target and (m == 0 or nums[m - 1] < target):
                    r1 = m
                    r2 = m
                    l += 1
                    break
            else:
                if nums[m] >= target:
                    r = m - 1
                else:
                    l = m + 1
                m = int((l + r) / 2)
        r = len(nums) - 1
        while (l <= r):
            if nums[m] == target and (m == r or nums[m + 1] > target):
                r2 = m
                break
            else:
                if nums[m] <= target:
                    l = m + 1
                else:
                    r = m - 1
                m = int((l + r) / 2)
        return [r1,r2]

别人代码

def searchRange(self, nums, target):
    def search(n):
        lo, hi = 0, len(nums)
        while lo < hi:
            mid = (lo + hi) / 2
            if nums[mid] >= n:
                hi = mid
            else:
                lo = mid + 1
        return lo
    lo = search(target)
    return [lo, search(target+1)-1] if target in nums[lo:lo+1] else [-1, -1]# 处理得很舒服

def searchRange(self, nums, target):
    lo = bisect.bisect_left(nums, target)
    return [lo, bisect.bisect(nums, target)-1] if target in nums[lo:lo+1] else [-1, -1]

思路

同类问题[Lintcode] 14. First Position of Target 但要注意,最后一个元素和第一个元素一个二分查找法是无法实现的。

  • 时间复杂度: O(log(n))

原文地址:https://www.cnblogs.com/siriusli/p/10358588.html

时间: 2024-08-11 18:02:17

[Lintcode]61. Search for a Range/[Leetcode]34. Find First and Last Position of Element in Sorted Array的相关文章

Leetcode 34 Find First and Last Position of Element in Sorted Array 解题思路 (python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第34题,这道题的tag是数组,需要用到二分搜索法来解答 34. Find First and Last Position of Element in Sorted Array Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target v

[leetcode][34] Find First and Last Position of Element in Sorted Array

34. Find First and Last Position of Element in Sorted Array 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)

19.2.2 [LeetCode 34] Find First and Last Position of Element in Sorted Array

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].

[二分搜索] leetcode 34 Find First and Last Position of Element in Sorted Array

problem:https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 经典二分搜索题.要点是改变low或high的时候把当前数字mid也包含进来,因为它也可能是结果. class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { vector<int

刷题34. Find First and Last Position of Element in Sorted Array

一.题目说明 题目是34. Find First and Last Position of Element in Sorted Array,查找一个给定值的起止位置,时间复杂度要求是Olog(n).题目的难度是Medium! 二.我的解答 这个题目还是二分查找(折半查找),稍微变化一下.target==nums[mid]后,需要找前面.后面的值是否=target. 一次写出来,bug free,熟能生巧!怎一个爽字了得! #include<iostream> #include<vecto

[LeetCode] 34. Search for a Range 搜索一个范围(Find First and Last Position of Element in Sorted Array)

原题目:Search for a Range, 现在题目改为: 34. Find First and Last Position of Element in Sorted Array 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

【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array

描述: 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, -

Search for a Range leetcode java

题目: 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

Search for a Range -- leetcode

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,