LeetCode - Refresh - Search for a Range
2015-03-23 12:56
337 查看
Actually, we are searching the right end of the target so:
1. start could be same as end
2. A[mid] > target, shift to left
3. A[mid] <= target shift to right
1. start could be same as end
2. A[mid] > target, shift to left
3. A[mid] <= target shift to right
class Solution { public: int bsearch(int A[], int start, int end, int target) { int index = -1, mid = 0; while (start <= end) { mid = (start + end)/2; if (A[mid] <= target) { start = mid + 1; index = mid; } else { end = mid - 1; } } return index; } vector<int> searchRange(int A[], int n, int target) { vector<int> result(2, -1); result[0] = bsearch(A, 0, n-1, target-1) + 1; result[1] = bsearch(A, 0, n-1, target); if (result[0] == -1 || A[result[0]] != target) { return vector<int> (2, -1); } return result; } };
相关文章推荐
- LeetCode | Search for a Range
- leetcode[34]Search for a Range
- LeetCode Num34_Search for a Range
- Search for a Range ——LeetCode
- Leetcode 34. Search for a Range (Medium) (cpp)
- leetcode 34 search for a range
- python写算法题:leetcode: Add to List 34. Search for a Range
- Leetcode-Search for a range
- [LeetCode] Search for a Range 解题报告
- LeetCode 34. Search for a Range
- LeetCode-Search for a Range
- Leetcode: Search for a Range
- Leetcode: 34. Search for range(Week9, Medium)
- LeetCode 034 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