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 

  • 相关阅读:
    Echrarts的基本API
    Echarts中Option属性设置
    大数据ETL处理时遇到的坑
    模块设计模式
    PAT (Advanced Level) Practice 代码
    递归中的 DFS 与 DP 比较
    CF 1557 D. Ezzat and Grid
    CF 1557 C. Moamen and XOR
    CF 1555 E. Boring Segments
    CF 1555 D. Say No to Palindromes
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6759494.html
Copyright © 2011-2022 走看看