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].

思路:

我自己用了个最简单的,普通的二分查找,然后两边延伸找起始和结束点。

int *searchRange(int A[], int n, int target) {
int ans[] = {-, -};
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(A[m] == target)
{
ans[] = ans[] = m;
while(ans[] - >= && A[ans[]] == A[ans[] - ]) ans[]--;
while(ans[] + < n && A[ans[]] == A[ans[] + ]) ans[]++;
return ans;
}
else if(A[m] > target)
r = m - ;
else
l = m + ;
}
return ans;
}

大神分成了分别用两次二分查找找起始和结束位置

https://leetcode.com/discuss/18242/clean-iterative-solution-binary-searches-with-explanation中有详细解释

vector<int> searchRange(int A[], int n, int target) {
int i = , j = n - ;
vector<int> ret(, -);
// Search for the left one
while (i < j)
{
int mid = (i + j) /;
if (A[mid] < target) i = mid + ;
else j = mid;
}
if (A[i]!=target) return ret;
else ret[] = i; // Search for the right one
j = n-; // We don't have to set i to 0 the second time.
while (i < j)
{
int mid = (i + j) / + ; // Make mid biased to the right
if (A[mid] > target) j = mid - ;
else i = mid; // So that this won't make the search range stuck.
}
ret[] = j;
return ret;
}

另一种通过加减0.5来找位置的方法

class Solution:
# @param A, a list of integers
# @param target, an integer to be searched
# @return a list of length 2, [index1, index2]
def searchRange(self, arr, target):
start = self.binary_search(arr, target-0.5)
if arr[start] != target:
return [-1, -1]
arr.append(0)
end = self.binary_search(arr, target+0.5)-1
return [start, end] def binary_search(self, arr, target):
start, end = 0, len(arr)-1
while start < end:
mid = (start+end)//2
if target < arr[mid]:
end = mid
else:
start = mid+1
return start

【leetcode】Search for a Range(middle)的更多相关文章

  1. 【leetcode】Swap Nodes in Pairs (middle)

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  2. 【leetcode】Linked List Cycle II (middle)

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  3. 【leetcode】Reverse Linked List II (middle)

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. 【leetcode】Path Sum I & II(middle)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  5. 【leetcode】Minimum Size Subarray Sum(middle)

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  6. 【leetcode】Evaluate Reverse Polish Notation(middle)

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. 【leetcode】Container With Most Water(middle)

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. 【LeetCode】10.Regular Expression Matching(dp)

    [题意] 给两个字符串s和p,判断s是否能用p进行匹配. [题解] dp[i][j]表示s的前i个是否能被p的前j个匹配. 首先可以分成3大类情况,我们先从简单的看起: (1)s[i - 1] = p ...

随机推荐

  1. WCF中安全的那些事!!!

    WCF默认绑定 WCF预先为所有绑定都定义了能满足大多数情形的配置模式,这样的话,只要没有修改某个配置参数,WCF就使用默认的安全模式. 先上一张默认的安全设置表格 绑定 设置 wsHttpBindi ...

  2. 分解大质数模板(复杂度小于sqrt(n))

    //POJ 1811 #include <cstdio> #include <cstring> #include <algorithm> #include < ...

  3. Java小程序--抓取emai

    一.实现思路 1.使用Java.net.URL对象,绑定网络上某一个网页的地址 2.通过java.net.URL对象的openConnection()方法获得一个HttpConnection对象 3. ...

  4. jQuery - 动态创建iframe并加载页面

    <html> <head> <script language="JavaScript" src="jquery-1.11.1.min.js& ...

  5. H5项目常见问题汇总及解决方案

    H5项目常见问题汇总及解决方案 H5   2015-12-06 10:15:33 发布 您的评价:       4.5   收藏     4收藏 H5项目常见问题及注意事项 Meta基础知识: H5页 ...

  6. 关于JavaScript中对象的继承实现的学习总结

    一.原型链 JavaScript 中原型链是实现继承的主要方法.其主要的思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.实现原型链有一种基本模式,其代码如下. function Super ...

  7. iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)

    一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...

  8. 3d回顾

    手把手教你玩转 CSS3 3D 技术   要玩转css3的3d,就必须了解几个词汇,便是透视(perspective).旋转(rotate)和移动(translate).透视即是以现实的视角来看屏幕上 ...

  9. 数据结构大二课程设计:QT实现线段树

    源码以及编译文件下载地址:http://download.csdn.net/detail/zhiyanpianyu1234/9445909#comment 加入了一些小东西,一直觉得课设是做给自己看的 ...

  10. GCD-Grand Central Dispatch

    经常要用的,总结分类一下. //获得队列 DISPATCH_QUEUE_PRIORTY 优先级 dispatch_queue_t Queue = dispatch_get_global_queue(D ...