Python
1 2 3 4 5 6 7 8 9 10 11 12 |
# Author: Huahua # Returns the smallest m in [l, r), # s.t. cond(m) == True # If not found returns r. def binarySearch(l, r) while l < r: m = l + (r - l) // 2 if cond(m): r = m else l = m + 1 return l |
C++
1 2 3 4 5 6 7 8 9 10 11 12 |
// Author: Huahua // Returns the smallest m in [l, r), // s.t. cond(m) == True // If not found returns r. int binarySearch(int l, int r) { while (l < r) { int m = l + (r - l) / 2; if (cond(m)) r = m; else l = m + 1; } return l; } |
Related Articles
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment