Python implementations of Lower_bound and Upper_bound__python

Source: Internet
Author: User
1. Lower_bound (nums, target)

In a non-descending array nums, Lower_bound (nums, target) returns the first value that is greater than or equal to target, if the elements in Nums are less than target (that is, elements that do not have >=target), Returns the length of the nums (that is, where the target should be inserted if it is to be inserted into the nums)

#coding =utf-8
#返回nums中第一个 >=target is a worthwhile location to return to Len (nums)
def lower_bound (nums, target) If all nums are smaller than target: Low
    , high = 0, Len (nums)-1
    pos = Len (nums) while
    low
Test :

Nums = [9, A, A, m, print   
lower_bound (nums), print
lower_bound (Nums, ten),
print Lowe R_bound (nums),
print Lower_bound (nums),
print lower_bound (nums, MB),
print Lower_bound (Nums, ),
print Lower_bound (Nums, 40)
Run Result:


2. Upper_bound (nums, target)

In a non-descending array nums, Upper_bound (nums, target) returns the position of the first value greater than target, if the element in Nums is less than or equal to target (that is, the element that does not have a >target), Returns the length of the nums (that is, where the target should be inserted if it is to be inserted into the nums)

#返回nums中第一个 >target, if Nums is not larger than target, return Len (nums)
def upper_bound (Nums, target): Low
    , high = 0, Len ( Nums)-1
    pos = Len (nums) while
    Low
Test:

Nums = [9, A, A, m, print 
upper_bound (nums), print
upper_bound (Nums, ten),
print Uppe R_bound (nums),
print Upper_bound (nums),
print upper_bound (nums, MB),
print Upper_bound (Nums, ),
print Upper_bound (Nums, 40)
Run Result:



C + + Lower_bound source code reference: http://www.cplusplus.com/reference/algorithm/lower_bound/

C + + Upper_bound source code reference: http://www.cplusplus.com/reference/algorithm/upper_bound/

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.