Given a big sorted array with positive integers sorted by ascending order. The array is so big so that you can not get the length of the whole array directly, and you can only access the kth number by ArrayReader.get(k) (or ArrayReader->get(k) for C++). Find the first index of a target number. Your algorithm should be in O(log k), where k is the first index of the target number.

Return -1, if the number doesn't exist in the array.

Example

Given [1, 3, 6, 9, 21, ...], and target = 3, return 1.

Given [1, 3, 6, 9, 21, ...], and target = 4, return -1.

分析:

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

如果找不到target,返回-1。

比如 nums数组:[ 1  4   5   7   8   10  15  20   30   50   80   90  100  200  300 400 ….],

这个很大很大的数组,要找20,由于数组很大,所以要先找到nums[index]的值大于20的,最快速的方法是找到nums(index)大于20的,

依次找nums[2], nums[4],nums[8],找到第一个大于20的数,为nums[8],  所以循环要从nums[0]作为起点,nums[8]作为终点.

/**
* Definition of ArrayReader:
*
* class ArrayReader {
* // get the number at index, return -1 if index is less than zero.
* public int get(int index);
* }
*/
public class Solution {
/**
* @param reader: An instance of ArrayReader.
* @param target: An integer
* @return : An integer which is the index of the target number
*/
public int searchBigSortedArray(ArrayReader reader, int target) {
int index = 1;
while (reader.get(index - 1) < target) {
index = index * 2;
}
int start = 0, end = index - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (reader.get(mid) < target) {
start = mid;
} else {
end = mid;
}
} if (reader.get(start) == target) {
return start;
} if (reader.get(end) == target) {
return end;
}
return -1;
}
}

lintcode 447 Search in a Big Sorted Array的更多相关文章

  1. lintcode 447 Search in a Big Sorted Array(倍增+二分)

    题意:给一个按照升序排序的正整数数组.这个数组很大以至于只能通过固定的接口ArrayReader->get(k)来访问第k个数.并且也没有办法得知这个数组有多大.找到给出的整数target第一次 ...

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

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

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

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

  4. [OJ] Find Minimum in Rotated Sorted Array

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

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

  6. 【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  ...

  7. [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. 这道 ...

  8. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

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

随机推荐

  1. qt5.4

    rm -f libQt5Qml.so.5.4.0 libQt5Qml.so libQt5Qml.so.5 libQt5Qml.so.5.4g++ -Wl,-O1,--sort-common,--as- ...

  2. firefox怎么修改tls协议号

    如果目前正在运行火狐26,你可能已经注意到,浏览器仅支持SSL 3.0和TLS 1.0,默认不开启TLS 1.1或TLS 1.2.另外我们知道Firefox 27 已经实现了对TLS 1.2的支持.  ...

  3. linux如何查看系统信息

    一:cpu [root@srv /]# more /proc/cpuinfo | grep "model name" model name    : Intel(R) Xeon(R ...

  4. 自然语言17_Chinking with NLTK

    https://www.pythonprogramming.net/chinking-nltk-tutorial/?completed=/chunking-nltk-tutorial/ 代码 # -* ...

  5. Android学习笔记——Handler(一)

    使用Handler管理线程(转) 步骤: 1. 申请一个Handler对象 Handler handler = new Handler(); 2. 创建一个线程 {继承Thread类或者实现Runna ...

  6. 移动端a标签点击图片有阴影处理

    移动端我们在点击页面中的一些图片的时候会出现阴影.处理方法只要给a标签加上 a { -webkit-tap-highlight-color: transparent; -webkit-touch-ca ...

  7. 个人js

    1.网页右侧地图浮动楼层,超过100px就显示 $(window).scroll(function(){ ){ //距顶部多少像素时,出现返回顶部按钮 $("#floor").fa ...

  8. GMU 简单使用一

    <!doctype html> <html> <head> <title>iOS7风格的进度条</title> <meta chars ...

  9. Python之路【第十二篇续】jQuery案例详解

    jQuery 1.jQuery和JS和HTML的关系 首先了HTML是实际展示在用户面前的用户可以直接体验到的,JS是操作HTML的他能改变HTML实际展示给用户的效果! 首先了解JS是一门语言,他是 ...

  10. android自定义控件(9)-Android触摸事件分发机制

    触摸事件的传递机制:   首先是最外层的viewgroup接收到事件,然后调用会调用自己的dispatchTouchEvent方法.如果在ACTION_DOWN的时候dispatchTouchEven ...