leetcode---Search for a Range
2016-05-26 15:41
267 查看
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].
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(vector<int>& nums, int target) { vector<int> ans; int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = upper_bound(nums.begin(), nums.end(), target) - nums.begin(); if(l < r) { ans.push_back(l); ans.push_back(r-1); } else { ans.push_back(-1); ans.push_back(-1); } return ans; } };
相关文章推荐
- Android中Activity切换动画
- ORACLE RMAN备份及还原
- 87. Nim Game
- Linux下静默安装oracle11G
- iOS之block浅谈
- mysql常用操作(二)
- 何时需要使用getMeasuredHeight()\getMeasuredWidth()?
- SQL 中 decode()函数 简介.
- 自定义View
- dhcp 协议原理与说明
- UriImageLoader加载图片
- 【gradle使用前篇—Groovy简介】
- Linux下tar解压缩命令
- 实现返回顶部按钮一直在页面底部(兼容ie6)
- 好消息! 不用再羡慕Python有jupyter 我R也有Notebook了【附演示视频】
- 利用poi导出excel
- Linux 代码格式化工具 indent
- 内存访问
- IntelliSense: "const char *" 类型的实参与 "LPCTSTR" 类型的形参不兼容
- JSPatch