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 

  • 相关阅读:
    Java单链表的实现
    leetcode2
    Spring容器中Bean的生命周期
    my-leetcode
    Map及HashMap原理简述
    网站搭建:服务器+域名+备案
    浅谈X-UA-Compatible&viewport
    Hexo框架搭建个人博客
    Redis安装教程
    MySQL安装教程 --- 解压版
  • 原文地址:https://www.cnblogs.com/whatyouknow123/p/6759494.html
Copyright © 2011-2022 走看看