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. 洛谷 P1957 口算练习题

    洛谷 P1957 口算练习题 题目描述 王老师正在教简单算术运算.细心的王老师收集了i道学生经常做错的口算题,并且想整理编写成一份练习. 编排这些题目是一件繁琐的事情,为此他想用计算机程序来提高工作效 ...

  2. 03009_SQL注入问题

    1.注入问题 (1)假设有登录案例SQL语句如下: SELECT * FROM 用户表 WHERE NAME = 用户输入的用户名 AND PASSWORD = 用户输的密码; (2)此时,当用户输入 ...

  3. Android eclipse 提示java代码 快捷键

    1.提示java代码能够用ALT+/ 键就能够了(前提是你要把你须要的类或方法的首字母打出来).我添加的部分:仅仅要输入sysout,然后alt+/键就能够输出System.out.prinln(), ...

  4. NPOI根据列索引获取列名

    public static string ConvertColumnIndexToColumnName(int index) { index = index + ; ; ]; ; ) { int mo ...

  5. slice深拷贝数组

    var a = [1, 2, 3, 4] var b = a.slice(0) b[0] = 2 // a = [1, 2, 3, 4] // b = [2, 2, 3, 4]

  6. BZOJ2716: [Violet 3]天使玩偶(KD-Tree)

    Description Input Output Sample Input 100 100 81 23 27 16 52 58 44 24 25 95 34 2 96 25 8 14 97 50 97 ...

  7. 洛谷——P1012 拼数

    https://www.luogu.org/problem/show?pid=1012#sub 题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整 ...

  8. Linux下守护进程精析

    什么是守护进程?     守护进程就是通常所说的Daemon进程,它是Linux中的后台服务程序. 它是一个生存期较长的进程,通常独立于终端而且周期性的运行某种须要的任务以及有时候会等待一些将会发生的 ...

  9. 《鸟哥的Linux私房菜-基础学习篇(第三版)》(四)

    第3章 主机规划与磁盘分区        1. Linux与硬件的搭配        首先谈了认识计算机的硬件配置. 然后谈了选择与Linux搭配的主机配置. 在Linuxserver中,内存的重要性 ...

  10. jvm compile

    >>>Making sec-files-win @ Thu Oct 17 20:34:02 CST 2013 ... >>>Making jgss-files @ ...