简体   繁体   English

在二分查找中,我们如何确定答案何时位于下限、上限和中间?

[英]How do we identify when the answer will be at lower bound, upper bound and at mid in a binary search?

I just started learning Binary search I have understood that when we are finding some exact element we return mid我刚开始学习二分搜索我知道当我们找到一些确切的元素时我们会返回 mid

but in some more advanced questions we have to return lower bounds and upper bounds, how do i identify that?但在一些更高级的问题中,我们必须返回下限和上限,我该如何识别? when to return what, I am getting super confused.什么时候归还什么,我变得非常困惑。

In a binary search youre finding a specified number from a SORTED array, so the first entry should be the lower bound and the last the upper bound.在二进制搜索中,您从 SORTED 数组中找到指定的数字,因此第一个条目应该是下限,最后一个条目应该是上限。 so depending on the language it would be something like:所以根据语言,它会是这样的:

array[0] // lower bound
array[array.length - 1] // upper bound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM