Questions dichotomy Summary

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_37907797/article/details/102755927

In the process of the brush in question, with the dichotomy or with a lot of overtime and sometimes you do not often spend dichotomy, today I came a little summary of up to three under with a binary search .

First, the target value is equal to the number of search and

This is the simplest type, for example, an array arr = {1, 2, 5, 6, 9}, the search returns to our target number target = 6, at this time we need to return the subscript i 6 = 3. code show as below

int binarySearch(int[] arr, int target){
    int left = 0, right = arr.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) return mid;
        else if (arr[mid] < target) left = mid + 1;
        else return mid;
    }
    return -1;
}

But this has a point to note is that many people, when seeking mid, and will write

mid = (left + right) / 2;

In fact, this writing is a little problem, because left + right could lead to a numeric overflow , so mid calculated on the mistakes, so often written like a small partner to pay attention, strict wording should be written like this:

mid = left + (right - left) / 2;

Second, find the first target of a number of not less than

Find the number is not less than the first target, I think the frequency of occurrence of this type is the highest, but also to note that the target number does not necessarily appear in the array.

For example, an array arr = {1, 2, 5, 6, 9}, the target number of target = 6, we want to return the subscript i = 4.

Another example arr = {0, 1, 2, 2, 2, 3}, target = 2. We want to return to i = 2.

code show as below

int binarySearch(int[] arr, int target){
    int left = 0, right = arr.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] < target) {
            left = mid + 1;
        }else{
            right = mid;
        }
    }
    return right;
}

In the code, the first and the biggest difference is the right = mid - 1 becomes right = mid, as to why? Make up their own brain, I just find the example cited above simulation on the line.

Yes, there is a and find the first target value is not less than the number of similar problems, is looking for the last number is less than the target value . It's very simple, direct return right - 1 on it.

Third, find the first number is greater than the target value

Find the first number is greater than the target, I think the frequency of occurrence of this type is the highest, but also to note that the target number does not necessarily appear in the array.

For example, an array arr = {1, 2, 5, 6, 9}, the target number of target = 6, we want to return to index i = 5.

Another example arr = {0, 1, 2, 2, 2, 3}, target = 2. We want to return to i = 5.

Let's look at the code

int binarySearch(int[] arr, int target){
    int left = 0, right = arr.length - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] <= target) {
            left = mid + 1;
        }else{
            right = mid;
        }
    }
    return right;
}

There are wooden and felt too much like the second type of code, and only need the if statement in it arr [mid] <target into arr [mid] <= target can be. As for why? Because of not less than a and the first greater than or equal one and the same meaning, but this question is the first one is greater than , not including the equal case ah. Therefore, the <to <= to.

to sum up

In fact, for finding dichotomy, there are many types, however, the code difference is not large, it is that there may be greater than or equal to change. left / right = mid + 1 becomes the left / right = mid. So sometimes very easy to make mistakes, especially when his mind in turmoil, it is recommended that I have here, choose a code style you like , then every time the question of the time, the share of mind with your template code.

Of course, I said above, these three still relatively simple, there are some more difficult, is the target may be dynamically updated, and updates left and right is also more complex, but for this, in general are hard leetcode level of the subject. Therefore, we can put all three to master, but when you do question, does not have a direct say what kind of questions above, then you do not even take into account the dichotomy can be used to do, so usually do question the when you can pay more attention.

Watching the harvest? So give me hope that the old iron trifecta

1, thumbs up , so that more people can see the article
2, public attention to my original number " hard to force the code farmers ", the first time to read my article. No public backstage reply " e-book " to send you a e-book gift package.
3, also welcomed the attention oh my blog

Author info

Author: handsome, a guy who loves writing
original public number: "hard to force the code farmers" to write more than 150 articles, focusing on writing algorithms, basic computer knowledge to enhance the internal strength of your article, look forward to your attention . No reply back public "electronic" send you an e-book gift package.
Reprinted Description : Be sure to indicate the source (note: from public number: hard to force the code farmers, author: handsome earth)

Guess you like

Origin blog.csdn.net/m0_37907797/article/details/102755927