61. Search for a Range【medium】
61. Search for a Range【medium】
Given a sorted array of n integers, find the starting and ending position of a given target value.
If the target is not found in the array, return [-1, -1]
.
Given [5, 7, 7, 8, 8, 10]
and target value 8
,
return [3, 4]
.
O(log n) time.
解法一:
- class Solution {
- public:
- /*
- * @param A: an integer sorted array
- * @param target: an integer to be inserted
- * @return: a list of length 2, [index1, index2]
- */
- vector<int> searchRange(vector<int> &A, int target) {
- if (A.empty()) {
- return vector<int>(, -);
- }
- vector<int> result;
- //find first
- int start = ;
- int end = A.size() - ;
- while (start + < end) {
- int mid = start + (end - start) / ;
- if (A[mid] == target) {
- end = mid;
- }
- else if (A[mid] < target) {
- start = mid;
- }
- else if (A[mid] > target) {
- end = mid;
- }
- }
- if (A[start] == target) {
- result.push_back(start);
- }
- else if (A[end] == target) {
- result.push_back(end);
- }
- else {
- return vector<int>(, -);
- }
- //find last
- start = ;
- end = A.size() - ;
- while (start + < end) {
- int mid = start + (end - start) / ;
- if (A[mid] == target) {
- start = mid;
- }
- else if (A[mid] < target) {
- start = mid;
- }
- else if (A[mid] > target) {
- end = mid;
- }
- }
- if (A[end] == target) {
- result.push_back(end);
- }
- else if (A[start] == target) {
- result.push_back(start);
- }
- return result;
- }
- };
61. Search for a Range【medium】的更多相关文章
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- 28. Search a 2D Matrix 【easy】
28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...
- 【Leetcode】【Medium】Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- codevs 4163 求逆序对的数目 -树状数组法
4163 hzwer与逆序对 时间限制: 10 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description hzwer在研究逆序对. 对于数列{a},如果 ...
- 数据挖掘经典算法——K-means算法
算法描述 K-means算法是一种被广泛使用的基于划分的聚类算法,目的是将n个对象会分成k个簇.算法的具体描述如下: 随机选取k个对象作为簇中心: Do 计算所有对象到这k个簇中心的距离,将距离最近的 ...
- iptables禁止外网访问redis server服务默认端口6379的命令
//只允许127.0.0.1访问6379 iptables -A INPUT -s 127.0.0.1 -p tcp --dport 6379 -j ACCEPT //其他ip访问全部拒绝 iptab ...
- spring+activity+mysql集群
第一步:先配置好第一个activityMQ 在broker外面加入数据库的连接信息,并将mysql的mysql-connector-java.jar,即java连接mysql的jar包放入apach ...
- @import url(../image/css)的用法
1.@import url(../image/css);可以加载css文件2.@import url(../image/css);可以写在html里加载css文件,也可以写在css文件里加载css文件 ...
- Lucene的学习及使用实验
实验一下Lucene是怎么使用的. 参考:http://www.importnew.com/12715.html (例子比较简单) http://www.yiibai.com/lucene/lucen ...
- Ubuntu16.04安装Pytorch
一.安装 1. 官方github:https://github.com/pytorch/pytorch Install optional dependencies //安装依赖项 On Linux e ...
- EasyUI-解决EasyUI 加载两次url的问题
1.传统方式 $(function () { var url = "../Source/Query/jhDataQry.ashx?action=query"; $(dg).data ...
- easyUI表头样式
easyUI表头样式 学习了:https://blog.csdn.net/lucasli2016/article/details/53606609 easyUI的样式定义在easyui.css中 表头 ...
- 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-Switch Case语句是否会自动跳转到下一个
在C#中,每一个case后面必须有break,所以输出1,也就是如果a=0,则只会执行case=0的那一段,当等于1之后不会继续. 在TwinCAT中,虽然CASE语句没有break,但是实际上不 ...