Your algorithm's runtime complexity must be in the order of O(log n).
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].
#include<iostream>
#include<vector>
using namespace std; vector<int> searchRange(int A[], int n, int target) {
int first = 0;
int last = n - 1;
vector<int>result(2, -1);
while (first<=last)
{
int mid = (first + last) / 2;
if (A[mid] == target)
{
result[0] = mid;
result[1] = mid;
while (result[0]-1 >= first&&A[result[0]-1] == target)//当一位是反复位时才对范围跟新
--result[0];
while (result[1]+1 <= last&&A[result[1]+1] == target)
++result[1];
return result;
}
else if (A[mid] < target)
first = mid + 1;
else
last = mid - 1;
}
return result;
}
- 本文已收录于下面专栏:
Your algorithm's runtime complexity must be in the order of O(log n).的更多相关文章
- Runtime Complexity of .NET Generic Collection
Runtime Complexity of .NET Generic Collection I had to implement some data structures for my compu ...
- LeetCode Algorithm
LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...
- [LeetCode] Search for a Range 搜索一个范围
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- BUG-FREE-For Dream
一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...
- 【leetcode】Search for a Range
题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...
- Leetcode Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- lastlog---显示系统中所有用户最近一次登录信息。
lastlog命令用于显示系统中所有用户最近一次登录信息. lastlog文件在每次有用户登录时被查询.可以使用lastlog命令检查某特定用户上次登录的时间,并格式化输出上次登录日志/var/log ...
- 解决Docker容器内访问宿主机MySQL数据库服务器的问题
懒得描述太多,总归是解决了问题,方法简要记录如下,虽然简要,但是完整,一来纪念处理该问题耗费的大半天时间,二来本着共享精神帮助其他遇到该问题的哥们儿,当然这个方法并不一定能解决你们的问题,但是多少能提 ...
- dp之完全背包 hdu--2159一维数组做法
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int ma ...
- 洛谷——P1498 南蛮图腾
https://www.luogu.org/problem/show?pid=1498 题目描述 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往 ...
- Poj1734题解
题目大意 求一个无向图的最小环 题解 假设是有向图的话.仅仅须要令f[i][i]=+∞,再floyd就可以: 对无向图.应该在floyd算法循环至k的一開始进行例如以下操作: 枚举i和j,假设点i存在 ...
- OPENSSL 制作 Ikev2证书
OPENSSL 制作 Ikev2证书 在一个 VPS 上配置 IKEV2 VPN 服务器时,用 OPENSSL 制作了所需的数字证书,奇怪的怎么弄都无法连接服务器,一直提示 "IKE_SA ...
- ajax嵌套ajax 可能出现问题 的解决办法
ajax由于他的异步特性 在第一次请求中的循环中嵌套第二个ajax会数据会读不出来 第一种 描述:如果条件许可,把两次请求都放在服务端处理掉一起发回来,这些就在客户端只有一次ajax了 优点:代码放在 ...
- ORA-16055: FAL request rejected
主库频繁报错如下: ORA-16055: FAL request rejected 解决办法: ALTER SYSTEM SET log_archive_dest_state_2='DEFER' ...
- SFC梯形图编程
SFC是居首的PLC编程语言 !: 不能为PLC所执行, 还需要其他的编程语言(梯形图) 转换成PLC可执行程序. 常用的SFC编程方法有三种 > 应用启保停电路进行 > 应用 置/复 ...
- Ubuntu配置sublime text 3的c编译环境
新建编译系统 c语言 选择tool –> Build System –> New Build System 然后输入下面代码 { "shell_cmd": " ...