leetcode brush Skills of the binary search template

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/qq_43152052/article/details/100155284

Premise binary search:
sequence needs to find is ordered, if disorderly, you will need to give the sort sequence, and then the binary search.

Binary search success three steps:

  • 1)Pretreatment: If the sequence is not sorted, the first sort
  • 2)Binary search: Using a loop or recursion intermediate element value is compared with the target element, will find one subinterval within interval into two subintervals, and then qualified until the end of the cycle or recursion.
  • 3)After-treatment: After the cycle is completed or recursion, meet the conditions necessary to determine the elements in the remaining zone of the element

Template 1:

int binarySearch(vector<int>& nums, int target)
{
  if(nums.size() == 0) return -1;
  int left = 0, right = nums.size() - 1;
  while(left <= right)
  {
  // Prevent (left + right) overflow
    int mid = left + (right - left) / 2;
    if(nums[mid] == target){ return mid; }
    else if(nums[mid] < target) { left = mid + 1; }
    else { right = mid - 1; }
  }
  // End Condition: left > right即left=right+1
  return -1;
}

Template 1 key attributes:

  • 1) binary search template 1 is the most basic and most basic form
  • 2) find an element can not be compared to determine the elements on both sides, i.e., left and up to right = mid-1, to the right look left = mid + 1
  • 3) post-treatment is not required, because after loop, the number of elements in the interval 0

Syntax differences:

  • 1) between the two partitions of [0, size-1]
  • 2) left and up right = mid-1; Finds the left = mid + 1
  • 3) End Cycling conditions: left> right i.e., left = right + 1

Template 2:

int binarySearch(vector<int>& nums, int target)
{
  if(nums.size() == 0)
    return -1;
  int left = 0, right = nums.size();
  while(left < right)
  {
    // Prevent (left + right) overflow
    int mid = left + (right - left) / 2;
    if(nums[mid] == target){ return mid; }
    else if(nums[mid] < target) { left = mid + 1; }
    else { right = mid; }
  }
  // Post-processing:
  // End Condition: left == right
  if(left != nums.size() && nums[left] == target) return left;
  return -1;
}      

Template 2 key attributes:

  • 1) Template 2 is a dichotomy advanced methods to find, which is used to find the need to access the array 当前索引and 其直接右邻居索引the elements or conditions (mid intermediate element, we not only need to determine the mid point of the element meets the conditions, we also need to determine the right mid of a neighbor element whether the condition: if the mid point of the element and the right neighbor elements mid + 1 to meet the conditions, we only need to determine the elements of the left mid meets the conditions just fine, so the next time to find the right boundary sub-interval of the right is the mid representatives of the elements, that is, right = mid; if the mid point of the elements when the conditions are not satisfied, then we also need to determine the right neighbor elements mid + 1 whether the conditions are met, we look for the right left = mid + 1 that is left to the mid right neighbor elements)
  • 2) find the range of at least two elements of the section
  • 3) the need to be processed, the end of the loop condition is left = right border point that is about the same element, we need to determine whether the subject of the provisions of this element

Syntax differences:

  • 1) between the two partitions of [0, size)
  • 2) Find the left: right = mid, look right: left = mid + 1
  • 3) the end of the Cycling conditions: left = right

Template 3:

int binarySearch(vector<int>& nums, int target){
    if (nums.size() == 0)
        return -1;
    int left = 0, right = nums.size() - 1;
    while (left + 1 < right)
    {
    // Prevent (left + right) overflow
        int mid = left + (right - left) / 2;
        if (nums[mid] == target) {return mid;}
        else if (nums[mid] < target) {left = mid;} 
        else {right = mid;}
    }
    // Post-processing:
    // End Condition: left + 1 == right
    if(nums[left] == target) return left;
    if(nums[right] == target) return right;
    return -1;
}
              

Template 3 key attributes:

  • 1) template-half 3 is another unique way to find, it needs access to the current index used to find its immediate neighbors about the elements or conditions index in the array (mid intermediate element, we not only need to determine whether the condition mid but also need to determine the mid of the neighbors whether the condition: if the mid to meet the conditions, we also need to determine left neighbor element mid satisfy a condition that the right = mid; if mid conditions are not satisfied, then we also need to determine the right of mid neighbor element meets the condition, i.e., left = mid)
  • 2) Find the section containing at least three elements
  • 3) the need to be processed, the end of the cycling conditions left + 1 = right, we need to determine the left and right element is directed to the target value

Distinguish syntax:

  • 1) between the two partitions of [0, size-1]
  • 2) to the left to find: right = mid, look to the right: left = mid
  • 3) the end of the Cycling conditions: left + 1 = right

Dichotomy exercises:

topic answer Difficulty
4. Looking for a median of two ordered arrays C++ hard
33. Search rotation sorted array C++ medium
34. Find a sorted array elements in the first and last position C++ medium
50. Pow(x, n) C++ mdium
69. x square root C++ easy
153. Find the minimum value in sorted array rotation C++ medium
154. Find the minimum value in sorted array rotating II C++ hard
162. Looking peak C++ medium
278. The first wrong version C++ easy
287. find the number of repetitions C++ medium
209. The minimum length of the sub-arrays C++ medium
349. The intersection of two arrays C++ easy
350. The two arrays of intersection II C++ easy
367. Effective perfect squares C++ easy
374. Guess Size C++ easy
410. The maximum value of a striped array C++ hard
454. The four numbers together II C++ medium
658. K closest element found C++ medium
704. Binary Search C++ easy
719. identify the small distance to the k C++ hard

Guess you like

Origin blog.csdn.net/qq_43152052/article/details/100155284