LeetCode-Search for a Range
来源:互联网 发布:五笔拆字软件 编辑:程序博客网 时间:2024/06/08 07:45
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 [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
Code:
<span style="font-size:14px;">class Solution {public: void binarySearch(int A[], int begin, int end, const int &target, vector<int> &result) { if (begin <= end) { int mid = (begin+end)/2; if (A[mid] == target) { result[0] = result[0]==-1?mid:min(result[0], mid); result[1] = max(result[1], mid); binarySearch(A, mid+1, end, target, result); binarySearch(A, begin, mid-1, target, result); } else if (A[mid] < target) binarySearch(A, mid+1, end, target, result); else binarySearch(A, begin, mid-1, target, result); } } vector<int> searchRange(int A[], int n, int target) { vector<int> result(2, -1); binarySearch(A, 0, n-1, target, result); return result; }};</span>
0 0
- LeetCode: Search for a Range
- LeetCode Search for a Range
- [Leetcode] Search for a Range
- LeetCode : Search for a Range
- [LeetCode] Search for a Range
- [Leetcode] Search for a Range
- Leetcode Search for a Range
- [LeetCode]Search for a Range
- LeetCode-Search for a Range
- [leetcode] Search for a Range
- LeetCode - Search for a Range
- Search for a Range leetcode
- LeetCode | Search for a Range
- LeetCode - Search for a Range
- LeetCode: Search for a Range
- leetcode: Search for a Range
- 【leetcode】Search for a Range
- Leetcode: Search for a Range
- POJ-1990-MooFest
- php中json
- 天安门金水桥换金色护栏 迎接新中国成立65周年
- linux Centos 6.5 安装桌面环境GNOME
- Oracle11g关于isqlplus服务
- LeetCode-Search for a Range
- va_list的用法
- Linux学习笔记(六)挂载使用文件系统
- asp.net总结(一)
- hdu 1085母函数的应用
- 【BZOJ】【P1337】【最小圆覆盖】【题解】【随机增量法】
- 多线程 学习笔记<1> 线程建立
- Android中图表AChartEngine学习使用与例子
- Linux 命令大全