[Leetcode][Python]34: Search for a Range

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘

34: Search for a Rangehttps://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 found in the array, return [-1, -1].For example,Given [5, 7, 7, 8, 8, 10] and target value 8,return [3, 4].

===Comments by Dabay===二分查找。当target在中间的时候,往两边扩展。‘‘‘

class Solution:    # @param A, a list of integers    # @param target, an integer to be searched    # @return a list of length 2, [index1, index2]    def searchRange(self, A, target):        def expend(nums, index):            left = right = index            while left - 1 >= 0 and nums[left - 1] == nums[index]:                left -= 1            while right + 1 < len(nums) and nums[right + 1] == nums[index]:                right += 1            return [left, right]

        l, r = 0, len(A) - 1        while l <= r:            m = (l + r) /2            if A[m] == target:                return expend(A, m)            elif A[m] < target:                l = m + 1            else:                r = m - 1        else:            return [-1, -1]

def main():    sol = Solution()    nums = [2,2]    target = 2    print sol.searchRange(nums, target)

if __name__ == ‘__main__‘:    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 02-01

[Leetcode][Python]34: Search for a Range的相关文章

[Leetcode + Lintcode] 34. 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 OJ 34. 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] 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 34.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

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

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 34 Search for a Range (C,C++,Java,Python)

Problem: 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, Giv

LeetCode 34. Search for a Range (找到一个范围)

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 not found in the array, return [-1, -1]. For e