[Offoffoff] q38: number of times a number appears in an array

Source: Internet
Author: User

Unlike the half-lookup mode, the difference is that the upper and lower boundary of a fixed value is not found here.

Def getbounder (data, K, start, end, low_bound = false): If end <start: Return-1 while start <= end: Mid = (start + end)> 1if data [Mid]> K: End = mid-1 Elif data [Mid] <K: Start = Mid + 1 else: If low_bound = true: if mid = 0 or data [Mid-1]! = K: Return midelse: End = mid-1 else: If mid = end or data [Mid + 1]! = K: Return midelse: Start = Mid + 1def getnumberofk (data, k): If Len (data) <= 1: Return Len (data) low_bound = getbounder (data, K, 0, Len (data)-1, low_bound = true) up_bound = getbounder (data, K, 0, Len (data)-1, low_bound = false) return up_bound-low_bound + 1


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.