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

最新文章

  1. requestAnimationFrame,Web中写动画的另一种选择
  2. LeetCode - Triangle
  3. addClass的用法和is函数的用法
  4. 使用Animation实现摄像机动画
  5. 排序之希尔排序(shell sort)
  6. iOS开发 AFNetworking 3.0使用遇到的问题
  7. [Twisted] transport和protocol解耦
  8. 百度编辑器ueditor如何配置
  9. gradient杂谈
  10. 完整的RecylerView的使用方法和例子
  11. deinstall oracle 11g on linux
  12. javaPNS进阶-高级推送技巧
  13. CoreCRM 开发实录 —— 单元测试、测试驱动开发和在线服务
  14. Map的迭代
  15. Android4.3 屏蔽HOME按键返回桌面详解(源码环境下)
  16. Android中SQLiteOpenHelper类的onUpgrade方法浅谈
  17. 使用yeoman搭建脚手架并发布到npm
  18. View动画(补间动画)
  19. Struck: Structured Output Tracking with Kernels
  20. 从Date类型字段获得当日周几的DAYNAME函数

热门文章

  1. 小米开源文件管理器MiCodeFileExplorer-源码研究(9)-入口分析
  2. springboot整合Beetl、BeetlSql实现ajax分页
  3. hdu5308 I Wanna Become A 24-Point Master(构造)
  4. Java IO:SocketChannel和Selector在ZooKeeper中应用
  5. 停止使用域名 boypay.net
  6. 华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root
  7. linux host主机名配置
  8. 至顶网推荐-Rpm另类用法加固Linux安全
  9. Writing buffer overflow exploits - a tutorial for beginners
  10. System.out.println 的多线程并发问题