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 

  • 相关阅读:
    锋利的BFC
    inline和inline-block的间隙问题
    margin和padding的四种写法
    js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结
    使用vscode自动编译less
    redux获取store中的数据
    react显示隐藏动画
    react使用路由
    react中使用fetchjsonp获取数据
    vue兼容到ie9
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6759494.html
Copyright © 2011-2022 走看看