Binary search 何时使用lo<;=高对低<;你好,二进制搜索?

Binary search 何时使用lo<;=高对低<;你好,二进制搜索?,binary-search,Binary Search,虽然二进制搜索是一种很好的算法,但我经常发现自己在二进制搜索的某些应用程序中遇到“off-by-1”问题 我见过二进制搜索的变体,它看起来像以下2种: while(lo <= hi) { // do stuff } while(lo一个简单的c#程序,用于显示“这与我的问题有什么关系?我在二进制搜索的上下文中询问每个问题的用法。如果这与问题无关,请在问题中添加更多信息,或者忽略这一点,因为我不理解你的问题。这可能会有所帮助 while(lo < hi) { // do st

虽然二进制搜索是一种很好的算法,但我经常发现自己在二进制搜索的某些应用程序中遇到“off-by-1”问题

我见过二进制搜索的变体,它看起来像以下2种:

while(lo <= hi)
{
  // do stuff
}

while(lo一个简单的c#程序,用于显示“这与我的问题有什么关系?我在二进制搜索的上下文中询问每个问题的用法。如果这与问题无关,请在问题中添加更多信息,或者忽略这一点,因为我不理解你的问题。这可能会有所帮助
while(lo < hi)
{
  // do stuff
}
   byte a = 2;

            for(byte b=1; b<4; b++)
            {
                if (a < b)
                {
                    Console.WriteLine($"{a} is less then {b}");
                }
                else
                {
                    Console.WriteLine($"{a} is not less then {b}");
                }
                if (a <= b)
                {
                    Console.WriteLine($"{a} is less or equal to {b}");
                }
                else
                {
                    Console.WriteLine($"{a} is not less or equal to {b}");
                }
            }
2 is not less then 1
2 is not less or equal to 1
2 is not less then 2
2 is less or equal to 2
2 is less then 3
2 is less or equal to 3