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 ...
随机推荐
- cnblogs的代码高亮
由于不喜欢cnblogs原来的代码高亮方案,于是自己瞎搞,外加看这位大神的blog以及BZOJ的代码高亮,终于是搞出来了...讲讲怎么弄吧. 当然对于了解css的大神可以无视以下文字…… 其实就是登上 ...
- 8.5(java学习笔记)8.5 字节码操作(javassist)
一.javassist javassist让我们操作字节码更加简单,它是一个类库,允许我们修改字节码.它允许java程序动态的创建.修改类. javassist提供了两个层次的API,基于源码级别的和 ...
- 一年的天数 Exercise06_16
/** * @author 冰樱梦 * 时间:2018年下半年 * 题目:一年的天数 * */ public class Exercise06_16 { public static void main ...
- Problem B: 调用函数,求1!+2!+3!+......+10!
#include<stdio.h> double fact(int i); int main() { int i; ; ;i<=;i++) sum=sum+fact(i); prin ...
- 个人python学习路线记录
一.入门视频 零基础入门学习Python --小甲鱼 二.博客园 python快速教程 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778. ...
- 乐观锁-version的使用
出处:http://chenzhou123520.iteye.com/blog/1863407 乐观锁介绍: 乐观锁( Optimistic Locking ) 相对悲观锁而言,乐观锁假设认为数据一般 ...
- Jackson使用ObjectManage#readValue传入泛型T的问题
说明:没找到合适的方法,持续关注这个问题 参考: https://stackoverflow.com/questions/11664894/jackson-deserialize-using-gene ...
- Android--使用XMLPull解析xml
在Android中极力推荐的xmlpull方式解析xml.xmlpull不只能够使用在Android上.相同也适用于javase,但在javase环境下.你须要自己去获取xmlpull所依赖的类库. ...
- javascript快速入门16--表格
表格的层次结构 <table border="1"> <caption>表格标题</caption> <thead> <tr& ...
- 安装Node.js、npm和环境变量的配置
由于Node.js平台是在后端运行JavaScript代码,所以,必须首先在本机安装Node环境. 一.安装Node.js 首先,从Node.js官网下载对应平台的安装程序,网速慢的童鞋请移步国内镜像 ...