zoukankan      html  css  js  c++  java
  • search for a range

    problem description:

      given a sorted aescend array,return the begining and ending position of the target num

      i.e:

      [1,2,3,8,8,9]

      return [3,4]

      of course, if you can not find the target in the array, just return [-1,-1]

    solution 1:

      you can use the binary search to find the first num which equal to target in the array,then use two index named i, j to find the max length

      there is my python solution

    class Solution(object):
        def searchRange(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            length = len(nums)
            low = 0 
            high = length - 1
            while low <= high:
                middle = (low+high)/2
                if nums[middle] == target:
                    i = middle
                    j = middle
                    while i>0 or j<length-1:
                        if i>0 and nums[i-1] == target:
                            i -= 1
                        elif j<(length-1) and nums[j + 1] == target:
                            j += 1
                        else:
                            break
                    return [i,j]
                elif nums[middle]<target:
                    low = middle +1
                else:
                    high = middle -1
            return [-1,-1]

    solution 2:

    there is another more clever solution which published in the leetcode discussion.

    the main idea is that you should fisrt find the start position,and then find the ending position.when find the start position you should make the middle seems prefer the left through make middle = (low+high)/2;when find the ending position you should make the middle seems prefer the right through make middle = (low+high)/2 +1

    there is the python solution:

    class Solution(object):
        def searchRange(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            ret = [-1, -1]
            low = 0
            length = len(nums)
            high = length - 1
            if length == 0:
                return ret
            while low < high:
                middle = (low+high)/2
                if nums[middle] < target:
                    low = middle +1
                else:
                    high = middle
            if nums[low] != target:
                return ret
            else:
                ret[0] = low
            high = length -1
            while low < high:
                middle = (low +high)/2 +1
                if nums[middle] > target:
                    high = middle -1
                else:
                    low = middle
            ret[1] = high 
            return ret

    finally, thanks to stellari bring out such a wonderful solution 

  • 相关阅读:
    拓扑排序笔记
    最小生成树——垃圾佬抓宠物
    次小生成树
    关于 海平面上升 与 fold的毒瘤题(easy) 的思考
    看正月点灯笼老师的笔记—01背包
    欧拉图的判定欧拉路的求法
    离散实验——关系闭包运算
    Floyd 求最短路
    离散实验——二元关系及其性质
    最小生成树
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6759494.html
Copyright © 2011-2022 走看看