题意:给一个按照升序排序的正整数数组。这个数组很大以至于只能通过固定的接口ArrayReader->get(k)来访问第k个数。并且也没有办法得知这个数组有多大。找到给出的整数target第一次出现的位置。你的算法需要在O(logk)的时间复杂度内完成,k为target第一次出现的位置的下标。如果找不到target,返回-1。

思路:倍增找到第一个大于target的位置,然后二分

 class Solve
{
int searchBigSortedArray(ArrayReader* reader,int target)
{
int index = ;
while(reader->get(index-)<target)
index*=;
int start=,end=index;
while(start<=end)
{
int mid = start+(end-start)/;
if(reader->get(mid)==target)
return mid;
else if(reader->get(mid)>target)
end = mid-;
else start = mid+;
}
return -;
}
};

lintcode 447 Search in a Big Sorted Array(倍增+二分)的更多相关文章

  1. lintcode 447 Search in a Big Sorted Array

    Given a big sorted array with positive integers sorted by ascending order. The array is so big so th ...

  2. 【刷题】Search in a Big Sorted Array

    原题戳我. 题目 Description Given a big sorted array with positive integers sorted by ascending order. The ...

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

  4. Find Minimum in Rotated Sorted Array 典型二分查找

    https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...

  5. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

  6. [OJ] Find Minimum in Rotated Sorted Array

    LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...

  7. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  8. 【Lintcode】062.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  ...

  9. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

随机推荐

  1. 安装mysql8.0出现服务无法启动,服务没报告任何错误

    改为net start mysql80 参考https://blog.csdn.net/gzejia/article/details/82156994

  2. Drag(拖拽)和Move(移动)两个脚本

    Drag using System.Collections; using System.Collections.Generic; using UnityEngine; public class Dra ...

  3. 使用item来封装数据:

    一.item和field类: 1.使用Item类: 创建了类Bookitem,然后就可以使用: 2.item_pipeline: 我们可以使用item_pipeline对爬取的数据进行处理. 步骤: ...

  4. redis-sentinel高可用配置(2)

    一:说明 前面我们已经配置了redis的主从配置(链接),这种主从架构有一个问题,当主master出现了故障了,怎么切换到从服务器上呢? 第一种:手动切换, 这种肯定会造成比较长一段时间的用户不能访问 ...

  5. OpenStack VS Kubernetes,谁是你心中的王者?

      当下云计算的领域里热度最高的两个项目,无疑是OpenStack和Kubernetes.如果云计算是一个风起云涌的江湖,毫不夸张的说OpenStack和Kubernetes就是江湖里的泰山北斗.Op ...

  6. CSS font字体知识学习

    字体系列 [1]5种通用字体系列:拥有相似外观的字体系列 serif字体:字体成比例,且有上下短线(衬线字体),包括Times\Georgia\New century Schoolbook sans- ...

  7. [再寄小读者之数学篇](2014-06-26 Besov space estimates)

    (1) $$\bex \sen{D^k f}_{\dot B^s_{p,q}}\sim \sen{f}_{\dot B^{s+k}_{p,q}}. \eex$$ (2) $$\beex \bea &a ...

  8. js检测移动设备并跳转到相关适应页面。

    PS:网页自适应的方式有多种.有通过CSS样式表来实现自适应(主流),也有通过显示不同的页面来实现的方式. 下面代码是记录通过判断设备特征来跳转到相关的页面的方法. 实现要求: 当手机,平板访问 a. ...

  9. spring和mybatis的整合开发(基于MapperFactoryBean的整合开发(方便简单不复杂))

    MapperFactoryBean是mybati-spring团队提供的一个用于根据mapper接口生成mapper对象的类. 在spring配置文件中可以配置以下参数: 1.mapperInterf ...

  10. sql 发送邮件

    一.启用Database Mail XPs功能. 查看Database Mail XPs功能是否打开,从返回结果来看,value为0说明没有打开,注意SQL Mail XPs是SQL Server早期 ...