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;
}

 

  • 本文已收录于下面专栏:

LeetCode Search for a Range搜索特定数值的范围 三种方法求解

在排序数组中搜索一个值有多少个。并返回其两边下标,没有找到就返回[-1,-1]。注意时间效率是O(logN)。这就肯定要用到二分法的思想了。

主要难度是处理好下标的走势。

有三种方法能够求解:

...

  • kenden23
  • 2013年12月03日 08:22
  • 1172

Search for a Range 有序数组里查找一个数的出现区间 @LeetCode

经典多次二分法。

package Level4;

import java.util.Arrays;

/**
* Search for a Range
*
* Given a sort...

  • hellobinfeng
  • 2013年11月10日 05:59
  • 3312

二分查找有序数组中某个数的所在范围 Search for a Range

题目源自于leetcode。

二分查找题。
题目:Given a sorted array of integers, find the starting and
ending position of ...

  • luckyjoy521
  • 2013年12月04日 20:38
  • 1484

A Route Search Method for Electric Vehicles in Consideration of Range

  • 2013年02月28日 14:51
  • 443KB
  • 下载

二分查找有序数组中某个数的所在范围 Search for a Range

题目源自于leetcode。

二分查找题。

题目:Given a sorted array of integers, find the starting and
ending position of ...

  • luckyjoy521
  • 2013年12月04日 20:38
  • 1484

[LeetCode-34] Search for a Range (寻找有序数组中关键值的索引范围)

系统输入參数必需要做推断
输入数组的长度和自己求解出来数组的长度不一致 int numslen= sizeof(nums)/sizeof(int); numsize !=numslen; /*这里特...
  • xy010902100449
  • 2015年09月30日 11:27
  • 435

LeetCode Search for a Range搜索特定数值的范围 三种方法求解

在排序数组中搜索一个值有多少个,并返回其两边下标,没有找到就返回[-1,-1]。注意时间效率是O(logN)。这就肯定要用到二分法的思想了。

主要难度是处理好下标的走势。

有三种方法能够求解:

...

  • kenden23
  • 2013年12月03日 08:22
  • 1172

Search for a Range 有序数组里查找一个数的出现区间 @LeetCode

经典多次二分法!

package Level4;

import java.util.Arrays;

/**
* Search for a Range
*
* Given a sort...

  • hellobinfeng
  • 2013年11月10日 05:59
  • 3312

LeetCode 之 Search for a Range — C++ 实现

Search for a Range

Given a sorted array of integers, find the starting and ending position of a gi...

  • abc123man
  • 2015年06月11日 14:15
  • 131

LeetCode OJ-34-Search for a Range

题目:Given a sorted array of integers, find the starting and ending position of a given target value. ...
  • dongtaizl
  • 2016年09月30日 11:15
  • 72

Your algorithm's runtime complexity must be in the order of O(log n).的更多相关文章

  1. Runtime Complexity of .NET Generic Collection

    Runtime Complexity of .NET Generic Collection   I had to implement some data structures for my compu ...

  2. LeetCode Algorithm

    LeetCode Algorithm 原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 1. Two Sum 3. Longest Substring Without Repea ...

  3. [LeetCode] Search for a Range 搜索一个范围

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  4. [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 ...

  5. BUG-FREE-For Dream

    一直直到bug-free.不能错任何一点. 思路不清晰:刷两天. 做错了,刷一天. 直到bug-free.高亮,标红. 185,OA(YAMAXUN)--- (1) findFirstDuplicat ...

  6. 【leetcode】Search for a Range

    题目描述: Given a sorted array of integers, find the starting and ending position of a given target valu ...

  7. Leetcode Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. lastlog---显示系统中所有用户最近一次登录信息。

    lastlog命令用于显示系统中所有用户最近一次登录信息. lastlog文件在每次有用户登录时被查询.可以使用lastlog命令检查某特定用户上次登录的时间,并格式化输出上次登录日志/var/log ...

  2. 解决Docker容器内访问宿主机MySQL数据库服务器的问题

    懒得描述太多,总归是解决了问题,方法简要记录如下,虽然简要,但是完整,一来纪念处理该问题耗费的大半天时间,二来本着共享精神帮助其他遇到该问题的哥们儿,当然这个方法并不一定能解决你们的问题,但是多少能提 ...

  3. dp之完全背包 hdu--2159一维数组做法

    #include <iostream>#include <stdio.h>#include <string.h>using namespace std;int ma ...

  4. 洛谷——P1498 南蛮图腾

    https://www.luogu.org/problem/show?pid=1498 题目描述 自从到了南蛮之地,孔明不仅把孟获收拾的服服帖帖,而且还发现了不少少数民族的智慧,他发现少数民族的图腾往 ...

  5. Poj1734题解

    题目大意 求一个无向图的最小环 题解 假设是有向图的话.仅仅须要令f[i][i]=+∞,再floyd就可以: 对无向图.应该在floyd算法循环至k的一開始进行例如以下操作: 枚举i和j,假设点i存在 ...

  6. OPENSSL 制作 Ikev2证书

    OPENSSL 制作 Ikev2证书 在一个 VPS 上配置 IKEV2 VPN 服务器时,用 OPENSSL 制作了所需的数字证书,奇怪的怎么弄都无法连接服务器,一直提示 "IKE_SA ...

  7. ajax嵌套ajax 可能出现问题 的解决办法

    ajax由于他的异步特性 在第一次请求中的循环中嵌套第二个ajax会数据会读不出来 第一种 描述:如果条件许可,把两次请求都放在服务端处理掉一起发回来,这些就在客户端只有一次ajax了 优点:代码放在 ...

  8. ORA-16055: FAL request rejected

    主库频繁报错如下: ORA-16055: FAL request rejected   解决办法: ALTER SYSTEM SET log_archive_dest_state_2='DEFER' ...

  9. SFC梯形图编程

    SFC是居首的PLC编程语言 !: 不能为PLC所执行, 还需要其他的编程语言(梯形图) 转换成PLC可执行程序. 常用的SFC编程方法有三种 >  应用启保停电路进行 >  应用 置/复 ...

  10. Ubuntu配置sublime text 3的c编译环境

    新建编译系统 c语言 选择tool –> Build System –> New Build System 然后输入下面代码 { "shell_cmd": " ...