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/