java 范围搜寻要怎么弄
Problem statement:
问题陈述:
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
给定一个以升序排列的整数nums数组,请找到给定目标值的开始和结束位置。
Your algorithm's runtime complexity must be in order to less than O(n).
您的算法的运行时复杂度必须小于O(n) 。
If the target is not found in the array, return [-1, -1].
如果在数组中找不到目标,则返回[-1,-1] 。
Constraints:
限制条件:
1 <= t <= 100
1 <= n <= 1000000
Example:
例:
Test case 1:
Input:
arr = [5,6,7,8,8,10]
target = 8
Output:
range is : [3, 4]
Test case 2:
Input:
arr = [5,7,7,8,8,10]
target = 6
Output:
range is: [-1,-1]
Explanation:
说明:
Though the above solutions are self-explanatory,
still for the first test case,
For the first test case,
Target is first present at index 3 and
last one at 4. (0-based indexing)
For the second test case,
Target is not at all present and
hence range is [-1,-1]
Solution approach
解决方法
It's quite similar to binary search. The modification is to not to stop when you get the target find. Search for the upper and lower elements to check if they are the same or not.
它与二进制搜索非常相似。 修改是在找到目标时不要停止。 搜索上部和下部元素以检查它们是否相同。
Here goes the full algorithm,
这里是完整的算法,
Initialize the result range to be [-1,-1]
初始化结果范围为[-1,-1]
If array size is 0
如果数组大小为0
return
返回
result;
结果 ;
Initialize left = 0, right = n-1;
初始化left = 0,right = n-1;
while(left<=right) mid=(left+right)/2; if(nums[mid]==target) //once found search for range set i=mid; set j=mid; while(i>=left && nums[i]==target) Decrease i; while(j<=right && nums[j]==target) Increase j; Result is [i+1,j-1] else if(nums[mid]<target) left=mid+1; else right=mid-1; end if End while
Result stores the starting and ending point of range. If target is not found then the range will be the initial one, [-1,-1]
结果存储范围的起点和终点。 如果找不到目标,则范围将是初始范围[-1,-1]
Let's solve with the first test case,
left = 0
right = 5
mid = 2
a[mid]<target, left = mid+1 = 3
a[mid] == target
so traverse left and right from this
range found to be [3,4]
For the second test case,
It simple comes out of the loop
C++ Implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
vector<int> searchRange(vector<int>& nums, int target)
{
int n = nums.size();
vector<int> p(2, -1);
if (n == 0)
return p;
int left = 0, right = n - 1;
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (nums[mid] == target) { //once found search for range
int i = mid;
int j = mid;
while (i >= left && nums[i] == target)
i--;
while (j <= right && nums[j] == target)
j++;
p[0] = i + 1;
p[1] = j - 1;
return p;
}
else if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return p;
}
int main()
{
int t;
cout << "Enter number of testcases\n";
cin >> t;
while (t--) {
int n;
cout << "Enter length of array\n";
cin >> n;
cout << "Enter the sorted vector\n";
vector<int> arr(n);
for (int i = 0; i < n; i++)
cin >> arr[i];
int k;
cout << "Enter target no\n";
cin >> k;
vector<int> result = searchRange(arr, n);
cout << "range is: [" << result[0] << "," << result[1] << "]\n";
}
return 0;
}
Output:
输出:
Enter number of testcases
2
Enter length of array
6
Enter the sorted vector
5 6 7 8 8 10
Enter target no
8
range is: [3,4]
Enter length of array
6
Enter the sorted vector
5 7 7 8 8 10
Enter target no
6
range is: [-1,-1]
翻译自: https://www.includehelp.com/icp/search-for-a-range.aspx
java 范围搜寻要怎么弄