Leetcode: Search for a Range
2014-02-11 23:59
253 查看
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return
For example,
Given
return
不难,关键是找到目标之后,怎么处理数组中都是相同元素的情况 - 也需要二分查找。
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
int low = 0, up = n - 1;
int mid;
while (low <= up) {
mid = (low + up) / 2;
if (A[mid] == target) {
break;
}
else if (A[mid] > target) {
up = mid - 1;
}
else {
low = mid + 1;
}
}
if (low > up) {
result.push_back(-1);
result.push_back(-1);
}
else {
// Handle the same elements in the array
result.push_back(findLeftBound(A, low, mid, target));
result.push_back(findRightBound(A, mid, up, target));
}
return result;
}
int findLeftBound(int A[], int left, int right, int target) {
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (A[mid] == target) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return left;
}
int findRightBound(int A[], int left, int right, int target) {
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (A[mid] == target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return right;
}
};也可以直接找最左和最右的元素。
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
int low = 0, up = n;
int mid;
while (low < up) {
mid = (low + up) / 2;
if (A[mid] >= target) {
up = mid;
}
else {
low = mid + 1;
}
}
if (A[low] != target) {
result.push_back(-1);
result.push_back(-1);
return result;
}
result.push_back(low);
low = 0, up = n;
while (low < up) {
mid = (low + up) / 2;
if (A[mid] <= target) {
low = mid + 1;
}
else {
up = mid;
}
}
result.push_back(up - 1);
return result;
}
};
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return
[-1, -1].
For example,
Given
[5, 7, 7, 8, 8, 10]and target value 8,
return
[3, 4].
不难,关键是找到目标之后,怎么处理数组中都是相同元素的情况 - 也需要二分查找。
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
int low = 0, up = n - 1;
int mid;
while (low <= up) {
mid = (low + up) / 2;
if (A[mid] == target) {
break;
}
else if (A[mid] > target) {
up = mid - 1;
}
else {
low = mid + 1;
}
}
if (low > up) {
result.push_back(-1);
result.push_back(-1);
}
else {
// Handle the same elements in the array
result.push_back(findLeftBound(A, low, mid, target));
result.push_back(findRightBound(A, mid, up, target));
}
return result;
}
int findLeftBound(int A[], int left, int right, int target) {
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (A[mid] == target) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
return left;
}
int findRightBound(int A[], int left, int right, int target) {
int mid;
while (left <= right) {
mid = (left + right) / 2;
if (A[mid] == target) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
return right;
}
};也可以直接找最左和最右的元素。
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
vector<int> result;
int low = 0, up = n;
int mid;
while (low < up) {
mid = (low + up) / 2;
if (A[mid] >= target) {
up = mid;
}
else {
low = mid + 1;
}
}
if (A[low] != target) {
result.push_back(-1);
result.push_back(-1);
return result;
}
result.push_back(low);
low = 0, up = n;
while (low < up) {
mid = (low + up) / 2;
if (A[mid] <= target) {
low = mid + 1;
}
else {
up = mid;
}
}
result.push_back(up - 1);
return result;
}
};
相关文章推荐
- [LeetCode] Search for a Range
- leetcode 34. Search for a Range (binary search std::lower_bound)
- leetcode 34 Search for a Range(二分法)
- leetcode:Search for a Range
- 【leetcode】Array——Search for a Range(34)
- leetcode第一刷_Search for a Range
- LeetCode Search for a Range
- Leetcode: Search for a Range
- Leetcode -- 34. Search for a Range
- Leetcode_34_Search for a Range
- LeetCode第34题之Search for a Range
- leetcode - Search for a Range
- Search for a Range - LeetCode 34
- LeetCode——Search for a Range
- [LeetCode]34. Search for a Range
- Search for a Range [LeetCode]
- [leetcode]Search for a Range
- 【LeetCode】34. Search for a Range
- (leetcode)Search for a Range
- 【leetcode】Search for a Range