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 ...
随机推荐
- Python 学习 第三天 课后总结:
PYTHON学习第三天课后总结: 1,注释:就是对代码起到说明注解的作用. 注释分为单行注释与多行注释. 单行注释:只注释一行代码在需要注释的所在行的行首使用#号来注释此行,注意#与代码之间需要 ...
- 【2017 Multi-University Training Contest - Team 2】TrickGCD
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=6053 [Description] 给你一个b数组,让你求一个a数组: 要求,该数组的每一位都小于等 ...
- Oracle Sqlplus中上下键出现^[[A乱码问题
安装rlwrap 下载:http://utopia.knoware.nl/~hlub/uck/rlwrap/ 或者 百度云盘:http://pan.baidu.com/s/1ntM8YXr 须要先安 ...
- code -结合实例总结代码下拉流程
1.查看手机需要的版本 1)如果手机本来就可以正常工作,可以使用指令 zhangshuli@zhangshuli-MS-:~/Desktop/day_note/plan$ adb shell getr ...
- Arrays.asList()方法的限制
Arrays.asList()方法的限制是他对所产生的List类型做出了最理想的假设 package example; import java.util.Arrays; import java.uti ...
- 4.使用fastjson进行json字符串和List的转换
转自:https://blog.csdn.net/lipr86/article/details/80833952 使用fastjson进行自定义类的列表和字符串转换 1.环境 jdk1.8,fastj ...
- GetListToJson
List<Models.ArticleModel> list = GetList(page); return Newtonsoft.Json.JsonConvert.Serializ ...
- golang 函数作为类型
golang 函数作为类型 package main import "fmt" type A func(int, int) func (f A)Serve() { fmt.Prin ...
- Android JNI用于驱动測试
硬件平台:S3C6410 操作系统:Ubuntu.windows 板子系统:Android 开发工具:jdk.ndk,eclipse 本次測试从linux内核模块编译開始.以S3C6410的pwm驱动 ...
- shell实例浅谈之三产生随机数七种方法
一.问题 Shell下有时须要使用随机数,在此总结产生随机数的方法.计算机产生的的仅仅是"伪随机数".不会产生绝对的随机数(是一种理想随机数).伪随机数在大量重现时也并不一定保持唯 ...