【LeetCode】Search for a Range

来源:互联网 发布:七政四余天星择日软件 编辑:程序博客网 时间:2024/05/19 14:00
class Solution {public:int Left(int A[], int n, int target){if (n == 0)return -1;int left = 0;int right = n - 1;while (left < right){int mid = left + (right - left) / 2;if (A[mid] >= target)right = mid;elseleft = mid + 1;}if (A[left] == target)return left;else return -1;}int Right(int A[], int n, int target){if (n == 0)return -1;int left = 0;int right = n - 1;while (left <=right){int mid = left + (right - left) / 2;if (A[mid] >target)right = mid-1;elseleft = mid + 1;}if (A[right] == target)return right;else return -1;}vector<int> searchRange(int A[], int n, int target) {vector<int> result;int  Findex,Sindex; Findex =Left(A, n, target);Sindex =Right(A, n, target); result.push_back(Findex);result.push_back(Sindex); return result;}};

0 0