Programming the beauty of the two-point search summary __ Programming

Source: Internet
Author: User

Binary search principle is very simple, but the boundary conditions prone to error, the death cycle, and so on, to be completely clear, you should understand thoroughly, we can use the first thought, and then use examples to verify, the following is my idea, if there are errors, please correct me. We first look at deformation one: if there are a lot of numbers to find, find the largest, so the largest must be at the far right, in order to be able to compare the left to two numbers, the end of the cycle, the left and right pointers corresponding to the number of 1, so the end of the cycle of the conditions to left < right-1; When data[mid] = = value, can not end, because data[mid+1] may also be equal to value, so to continue the traversal, then left = Mid, cannot let right=mid, because the right side of the mid may also have a value equal to values, So less than and equals to merge. Variant two are similar. Look again deformation three: in order to find Data[i]<value max I, when Data[i]==value can not end, at this time, because I must be equal to the value of the subscript left, so when Data[i]==value, to put right = Mid continues to traverse, that is, to merge greater than and equal, and at this time is different from the transformation, to find is less than, not equal to, so when data[i]>=value, the result is certainly on the left of I, it is impossible to include I, so right = Mid-1 instead of mid, deformation four similar.

Standard two-point find one:

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size () -1;//Note 1 while
	(left & lt;= right)//Note 2
	{
		int mid = left + ((right-left) >> 1);//Note 3
		if (Data[mid] < value) left = mid + 1;< C6/>else if (Data[mid] > value) right = mid-1;//Note 4
		else return mid;
	}
	return-1;
}

Two of the standard find two (not commonly used):

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size ();//NOTE 1 while
	(left < right)//Note 2
	{
		int mid = left + ((right-left) >> 1);
		if (Data[mid] < value) left = mid + 1;
		else if (Data[mid] > value) right = mid;//Note 3
		else return mid;
	}
	return-1;
}

Variant One: If there are more than one satisfying condition, return the maximum number of

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size () -1;//Note 1 while
	( Left < Right-1)
	{
		int mid = left + ((right-left) >> 1);
		if (Data[mid] <= value) left = mid;//Note 2
		Else right = mid;//Note 3
	}
	if (data[right] = = value) return right;< C20/>if (Data[left] = = value) return left;
	return-1;
}

Deformation two: If there are more than one satisfies the condition, returns the lowest ordinal

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size () -1;//Note 1 while
	(left & Lt Right-1)
	{
		int mid = left + ((right-left) >> 1);
		if (Data[mid] >= value) right = mid;//Note 2
		else left = mid;//Note 3
	}
	if (data[left] = = value) return left;
	if (data[right] = = value) return right;
	return-1;
}

Variant Three: Find the largest I, make Data[i] < value

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size () -1;//Note 1 while
	( Left < Right-1)
	{
		int mid = left + ((right-left) >> 1);
		if (Data[mid] < value) left = mid;//Note 2
		Else right = mid-1;//Note 3
	}
	if (Data[right] < value) return Right;
	if (Data[left] < value) return left;
	return-1;
}


Variant four: Find the smallest I, make data[i] > value

int Bisearch (vector<int>& data,int value)
{
	int left = 0,right = Data.size () -1;//Note 1 while
	(left & Lt Right-1)
	{
		int mid = left + ((right-left) >> 1);
		if (Data[mid] > value) right = mid;//Note 2
		else left = mid + 1;//Note 3
	}
	if (Data[left] > value) return Left;
  if (Data[right] > value) return right;
	return-1;
}

Leetcode Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would is if it were inserted in order.

Assume no duplicates in the array.

Here is few examples.
[1,3,5,6], 5→2
[1,3,5,6], 2→1
[1,3,5,6], 7→4
[1,3,5,6], 0→0

Class Solution {public
:
    int searchinsert (int a[], int n, int. target) {
    	int left = 0,right = n-1;
    	while (left < right-1)
    	{
    		int mid =left + ((right-left) >>1);
    		if (A[mid] > target) right = Mid-1;
    		else left = mid;
    	}
    	if (a[right] = = target) return right;
    	if (A[right] < target) return right+1;
    	if (A[left] >= target) return left;
    	return left+1;
    }
;


Search for a Range

Given a sorted array of integers, find the starting and ending position of a Given target value.

Your algorithm ' s runtime complexity must is in the order of O (log n).

If the target is not a found in the array, return [-1,-1].

For example,
Given [5, 7, 7, 8, 8, ten] and target value 8,
return [3, 4].

Class Solution {public
:
    vector<int> searchrange (int a[], int n, int target) {
    	int start = 0,end = n-1;< c3/>vector<int> Res (2);
    	while (Start < end-1)//Find start position
    	{
    		int mid = start + ((End-start) >>1);
    		if (A[mid] >= target) end = mid;
    		else start = mid + 1;
    	}
    	if (a[start] = = target)
    	{
    		res[0] = start;
    	}
    	else if (a[end] = = target)
    	{
    		res[0] = end;
    		start = end;
    	}
    	else 
    	{
    		res[0] = res[1] =-1;
    		return res;
    	}
    	end = N-1;
    	while (Start < end-1)//Find end position
    	{
    		int mid = start + ((End-start) >>1);
    		if (A[mid] <= target) start = mid;
    		else end = Mid-1;
    	}
    	if (a[end] = = target) res[1] = end;
    	else if (a[start]== target) Res[1]=start;
    	return res;
    }
};

LEETCDE's Submission Details

Write an efficient algorithm, searches for a value in a m x n matrix. This matrix has the following properties:

Integers in each row is sorted from the left to the right. The first integer of each row was greater than the last integer of the previous row.

For example,

Consider the following matrix:

[
  1,   3,  5,  7],
  [Ten, one, +, +], [
max., +]]

Given target = 3, return true. Idea: Starting from the top right corner, first two points to find which line, the binary find belongs to find the first greater than the target type, and then on the line for a normal binary search.

Class Solution {public
:
    bool Searchmatrix (vector<vector<int> > &matrix, int target) {
    	int rows = Matrix.size ();
    	if (rows <= 0) return false;
    	int cols = Matrix[0].size ();
    	if (cols <= 0) return false;
    
    	int up = 0,bottom = rows-1;
    	while (up < bottom-1)//binary finds the target in which line
    	{
    		int mid = up + ((bottom-up) >>1);
    		if (Matrix[mid][cols-1] > target) bottom = mid;
    		else if (Matrix[mid][cols-1] < target) up = mid + 1;
    		else return true;
    	}
    	if (Matrix[up][cols-1] < target) up = bottom;
    	int left = 0,right = cols-1;
    	while (left <= right)//binary finds in which column
    	{
    		int mid = left + ((right-left) >>1);
    		if (Matrix[up][mid] > target) right = Mid-1;
    		else if (Matrix[up][mid] < target) left = mid + 1;
    		else return true;
    	}
    	return false;
    }
};

For a rotating array of binary lookups, see here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.