Start index number of a number in the sorting Array
The questions are as follows:
Given an integer array in ascending order, search for index numbers that appear in an array. For example, if the number of search results is 3, 2, 3, 4, and 5, then 1 and 2 are returned. The time complexity must be O (logN ).
When you get this question for the first time, you can immediately use a binary search. First, compare the number in the middle and the number to be searched. If the keyword (number to be searched) is smaller than the number in the middle, the search continues in the left half of the array. If the keyword is greater than the number in the middle, the search continues in the right half of the array. If the keyword is equal to the number in the middle, first, compare whether the first digit of the intermediate digit is equal to the keyword. If it is equal, continue to use the keyword to compare with the previous digit of the previous digit. If not, the current number is the number to be searched, and its index is the first place to appear. For an ending index, you can use a similar method to compare whether the last digit of the intermediate number is equal to the keyword. If it is equal, continue to use the keyword to compare with the next digit of the next digit. If the difference is not the same, the current digit is the number to be searched, and its index is the last place to appear. However, in the worst case, the time complexity degrades to O (N), that is, when the array is the same number. Therefore, this method is not time-optimal.
In fact, this question is a variant of binary search. We can do this in two steps. The first step is to obtain the index of the first occurrence of the number, and the second step is to obtain the index of the last occurrence of the number.
First, let's look at how to find the index that appears for the first time. First, compare the number in the middle and the number to be searched. If the keyword (number to be searched) is smaller than the number in the middle, the search continues in the left half of the array. If the keyword is greater than the number in the middle, the search continues in the right half of the array. If the keyword is equal to the number in the middle, then compare whether the first digit of the intermediate digit is equal to the keyword. If not, the current intermediate index is the first index that appears. If it is equal, continue to the first half of the search. The specific implementation code is as follows:
// Search for start index int GetFirstTarget (int A [], int n, int target, int nStart, int nEnd) {if (nStart> nEnd) {return-1 ;} // intermediate index int nMid = nStart + (nEnd-nStart)> 1); int nMidData = A [nMid]; while (nStart <= nEnd) {if (target> nMidData) {nStart = nMid + 1;} else if (target <nMidData) {nEnd = nMid-1;} else if (target = nMidData) {if (target! = A [nMid-1] & nMid> 0) | nMid = 0) {return nMid;} elsenEnd = nMid-1 ;} // value index and value nMid = nStart + (nEnd-nStart)> 1); nMidData = A [nMid];} return-1 ;}
You can use a similar idea to find the last index: first, compare the number in the middle and the number to be searched. If the keyword (the number to be searched) is smaller than the number in the middle, the search continues in the left half of the array. If the keyword is greater than the number in the middle, the search continues in the right half of the array. If the keyword is equal to the number in the middle, then, compare the second digit of the intermediate digit to whether it is equal to the keyword. If not, the current intermediate index is the last index that appears. If it is equal, continue searching in the second half. When looking for the specific differences between the first index and the last index, pay attention to the words in red. The specific implementation code is as follows:
// Search for the end index int GetSecondTarget (int A [], int n, int target, int nStart, int nEnd) {if (nStart> nEnd) {return-1 ;} // intermediate index int nMid = nStart + (nEnd-nStart)> 1); int nMidData = A [nMid]; while (nStart <= nEnd) {if (target> nMidData) {nStart = nMid + 1;} else if (target <nMidData) {nEnd = nMid-1;} else if (target = nMidData) {if (target! = A [nMid + 1] & nMid <n) | nMid = N-1) {return nMid;} elsenStart = nMid + 1 ;} // value index and value nMid = nStart + (nEnd-nStart)> 1); nMidData = A [nMid];} return-1 ;}
Finally, the main function is called. The Code is as follows:
vector
searchRange(int A[], int n, int target){std::vector
vecIndex;vecIndex.resize(2);vecIndex[0] = -1;vecIndex[1] = -1;if (A == NULL || n <= 0){return vecIndex;}vecIndex[0] = GetFirstTarget(A,n,target,0,n-1);vecIndex[1] = GetSecondTarget(A,n,target,0,n-1);return vecIndex;}
The time complexity of the two searches is O (logN), so the total time complexity is O (logN ).
Finally, another variant of this question is the number of times the number appears in the SORT array.