Given an integer array sorted in ascending order, write a function to search target in nums.  If target exists, then return its index, otherwise return -1. However, the array size is unknown to you. You may only access the array using an ArrayReader interface, where ArrayReader.get(k) returns the element of the array at index k (0-indexed).

You may assume all integers in the array are less than 10000, and if you access the array out of bounds, ArrayReader.get will return 2147483647.

Example 1:

Input: array = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4

Example 2:

Input: array = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1

Note:

  1. You may assume that all elements in the array are unique.
  2. The value of each element in the array will be in the range [-9999, 9999].

这道题给了我们一个未知大小的数组,让我们在其中搜索数字。给了我们一个ArrayReader的类,我们可以通过get函数来获得数组中的数字,如果越界了的话,会返回整型数最大值。既然是有序数组,又要搜索,那么二分搜索法肯定是不二之选,问题是需要知道数组的首尾两端的位置,才能进行二分搜索,而这道题刚好就是大小未知的数组。所以博主的第一个想法就是先用二分搜索法来求出数组的大小,然后再用一个二分搜索来查找数字,这种方法是可以通过OJ的。但其实我们是不用先来确定数组的大小的,而是可以直接进行搜索数字,我们实际上是假设数组就有整型最大值个数字,在多余的位置上相当于都填上了整型最大值,那么这也是一个有序的数组,我们可以直接用一个二分搜索法进行查找即可,参见代码如下:

// Forward declaration of ArrayReader class.
class ArrayReader; class Solution {
public:
int search(const ArrayReader& reader, int target) {
int left = , right = INT_MAX;
while (left < right) {
int mid = left + (right - left) / , x = reader.get(mid);
if (x == target) return mid;
else if (x < target) left = mid + ;
else right = mid;
}
return -;
}
};

类似题目:

Binary Search

类似题目:

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/discuss/171669/Straight-forward-binary-search.

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/discuss/151685/Shortest-and-cleanest-Java-solution-so-far...

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Search in a Sorted Array of Unknown Size 在未知大小的有序数组中搜索的更多相关文章

  1. 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...

  2. LeetCode 702. Search in a Sorted Array of Unknown Size

    原题链接在这里:https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/ 题目: Given an integer ...

  3. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

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

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

  5. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

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

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

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

  8. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

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

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. C#使用ServiceStack读写Redis

    通过C#第三方库向Redis存储数据遇到的几个问题 https://github.com/ServiceStack/ServiceStack.Redis 1.将对象转json字符串 JsonObjec ...

  2. CentOS安装VLC

    For EL7: rpm -Uvh https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm rpm - ...

  3. Dijkstra算法的C++实现

    Dijkstra算法是在图中寻找两顶点最短路径的算法.   下面以下图有向图为例,说明其基本思想. 上图为转化为邻接矩阵存储:     现在我要寻找1点到其他点的最短距离以及路径: a)1点到各点的距 ...

  4. react-router v4 按需加载的配置方法

    在react项目开发中,当访问默认页面时会一次性请求所有的js资源,这会大大影响页面的加载速度和用户体验.所以添加按需加载功能是必要的,以下是配置按需加载的方法: 安装bundle-loader np ...

  5. nginx 中用 sed 批量增加配置文件内容

    #!/bin/bash confs=`ls /etc/nginx/conf.d/cms_vhosts/` cd /etc/nginx/conf.d/cms_vhosts for log in $con ...

  6. 查看oracle当前的连接数

    SQL> select count(*) from v$session #当前的连接数SQL> Select count(*) from v$session where status='A ...

  7. @ReponseBody返回的json中文乱码-遁地龙卷风

    我在mvc配置文件中加上下面这个配置就好了 <mvc:annotation-driven></mvc:annotation-driven>,需要在开头引用如下命名空间xmlns ...

  8. bat执行python脚本,执行多条命令

    1.新建一个txt文档,输入以下命令 @echo offcmd /k python F:\Pycharm_Projection\Test\test2.py 2.将txt文档保存为.bat格式,然后双击 ...

  9. jsonp跨域ajax跨域get方法

    原理: 就是利用<script >标签没有跨域限制的,从而达到与第三方网站通讯的目的.当需要通讯时,本站脚本创建一个<script>标签,src地址指向第三方网站的的一个网址. ...

  10. HTML5 图片下载

    1. 概述 1.1 说明 在项目过程中,有时候需要下载某一展示图片,html5中定义了<a> download属性,download属性规定被下载的超链接目标,该属性可以设置一个值来规定下 ...