Dichotomy Summary
Some leetcode by the above topics summarize the dichotomy algorithm
- Ordered Binary Search [no duplicate elements].
- Order to determine the boundary [with Repetition]
- Non-ordered search under [no duplicate elements].
- Determining the non-ordered boundary [duplicate elements].
Ordered Binary Search
From an ordered array (each element preferably is not repeated, it is a general description of the same border to search for it) which determines whether the element is present, there is a return to the standard, the absence of return -1.
template
//模板1:
int binarySearch(int []nums, int target)
{
int lo = 0, hi = nums.length - 1;
while(lo <= hi)
{
int mid = lo + (hi - lo) / 2;
if(nums[mid] < target)
lo = mid + 1;
else if(nums[mid] > target)
hi = mid - 1;
else
return mid;
}
return -1;
}
//模板2:
int binarySearch(int []nums, int target)
{
int lo = 0, hi = nums.length;
while(lo < hi)
{
int mid = lo + (hi - lo) / 2;
if(nums[mid] < target)
lo = mid + 1;
else if(nums[mid] > target)
hi = mid;
else
return mid;
}
return -1;
}
The difference between the two templates
The first template to ensure that each section is a look 闭区间
, while the second template to ensure that each section is a look 左闭右开的区间
. Two templates can be used.
topic
Ordered determine the boundaries
Among the elements of an ordered array of search time, when not immediately find the time to stop ( 因为元素重复的情况存在
), but should be based on the search of the left or right boundary is determined to continue the search to the left or right, among the rest of the search process if you can find the current element, index value is updated.
template
Only here 左闭右开区间
's what you can change the template:
//寻找左边界为例子.
int binarySearch(int []nums, int target)
{
int ans = -1;
int lo = 0, hi = nums.length;
while(lo < hi)
{
int mid = lo + (hi - lo) / 2;
if(nums[mid] < target)
lo = mid + 1;
else if(nums[mid] > target)
hi = mid;
else
{
ans = mid;
hi = mid; //如果是右边界, 则为 lo = mid + 1;
}
}
return ans;
}
topic
Under the non-ordered search
This situation is no longer an orderly array, we need our own analysis to arrive at a search the next interval is left or right, conditions are still using nums[mid]
to judge, but compare with undetermined factors, need a closer look.
template
Using the 左闭又开
template to be modified, the following pseudo-code:
int binarySearch(int nums[], int target)
{
int lo = 0, hi = nums.length;
while(lo < hi)
{
int mid = lo + (hi - lo) / 2;
/*
根据 nums[mid] 和 target 等因素判断target 在mid的左边还是
右边, 此时不能仅仅的比较大小了,根据所见问题可以有以下思路参考:
1. 非有序数组对半分后,一半有序一半无序,则在有序二分搜索,无序
部分继续对半,这需要使用递归的思想了。 [leetcode 33]
2. 判断 mid 和 target 是否处于同一个有序范围内, 在则根据nums
[mid] 和 target 大小比较确定下一个区间, 不在则想办法向
target所在的区间缩小。 [leetcode 33]
3. 如果没有给定target值, 比如说 leetcode 153 这种情况, 则需要
判断nums[i]则需要和相邻元素比较才能确定, 而没有具体targe值
来判断是否相等了。 [leetcode 153]
*/
}
return -1;
}
topic
Non boundary in order to determine the
Under non-ordered conditions, each element can be repeated, then the basic elements found in the previous case, the need to find this element boundaries job.
(Use 左闭又开模板
) in this case, when there is an element of equal, if looking for the left margin, then hi = mid
, if you find the right boundary, then lo = mid + 1
;