您的位置:首页 > 其它

Search for a Range

2012-10-28 14:30 190 查看
首先我要在纸上,非常非常聪明且迅速且机灵,
给出几个用例,找出边界用例和特殊用例,确定特判条件;在编码前考虑到所有的条件
向面试官提问:问题规模,特殊用例
给出函数头
暴力解,简述,优化。
给出能够想到的最优价
伪代码,同时结合用例
真实代码

Search for a Range

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]
.

http://collabedit.com/9k2mn

pair<int, int> search_range (vector<int> arr, int x)
{
return make_pair(upper_bound(arr.begin(), arr.end(), x), lower_bound(arr.begin(), arr.end(), x));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: