题目地址:https://leetcode-cn.com/problems/search-in-a-sorted-array-of-unknown-size/

题目描述

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:

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

Example 2:

  1. Input: array = [-1,0,3,5,9,12], target = 2
  2. Output: -1
  3. 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].

题目大意

给定一个升序整数数组,写一个函数搜索 nums 中数字 target。如果 target 存在,返回它的下标,否则返回 -1。注意,这个数组的大小是未知的。你只可以通过 ArrayReader 接口访问这个数组,ArrayReader.get(k) 返回数组中第 k 个元素(下标从 0 开始)。

你可以认为数组中所有的整数都小于 10000。如果你访问数组越界,ArrayReader.get 会返回 2147483647。

解题方法

遍历

直接可以线性遍历,如果ArrayReader不结束,那么一直向后查找,忽略掉题目给出的数组是有序的特征。

这样时间复杂度是O(N),事实证明题目给出的ArrayReader长度不长,下面的做法超过了96%的提交。

C++代码如下:

  1. // Forward declaration of ArrayReader class.
  2. class ArrayReader;
  3. class Solution {
  4. public:
  5. int search(const ArrayReader& reader, int target) {
  6. int index = 0;
  7. while (reader.get(index) != 2147483647) {
  8. if (reader.get(index) == target)
  9. return index;
  10. index ++;
  11. }
  12. return -1;
  13. }
  14. };

二分查找

既然题目说了数组是有序的,就是提示我们用二分查找。因为数组是递增且不同的,所以总的元素个数应该小于20000.

然后使用标准的二分模板写一遍就好了,注意这个区间是左开右闭。

这个时间复杂度是O(log(N)),但是提交后发现运行时间反而变慢,超过了39%的用户。

C++代码如下:

  1. // Forward declaration of ArrayReader class.
  2. class ArrayReader;
  3. class Solution {
  4. public:
  5. int search(const ArrayReader& reader, int target) {
  6. int left = 0;
  7. int right = 20010;
  8. // [left, right)
  9. while (left < right) {
  10. int mid = left + (right - left) / 2;
  11. int val = reader.get(mid);
  12. if (val == target) {
  13. return mid;
  14. } else if (val < target) {
  15. left = mid + 1;
  16. } else {
  17. right = mid;
  18. }
  19. }
  20. return -1;
  21. }
  22. };

日期

2019 年 9 月 24 日 —— 梦见回到了小学,小学已经芳草萋萋破败不堪

【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)的更多相关文章

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

  2. [LeetCode] Search in a Sorted Array of Unknown Size 在未知大小的有序数组中搜索

    Given an integer array sorted in ascending order, write a function to search target in nums.  If tar ...

  3. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  4. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  5. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  6. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  7. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  8. Java for LeetCode 081 Search in Rotated Sorted Array II

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

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

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

随机推荐

  1. 【豆科基因组】普通豆/菜豆/四季豆Common bean (Phaseolus vulgaris L.) 基因组

    目录 研究一:G19833组装,2014NG 研究二:BAT 93组装,2016 genome biology 菜豆属(Phaseolus L.)为同源二倍体作物,包含有80 多个物种,多数为野生种, ...

  2. NCBI SRA数据如何进行md5校验?

    下了一些sra数据库中的公共数据,因为pretech和aspera不稳定,稍微大点的文件经常传断,部分文件我只能通过本地下载再上传. 那么问题来了,sra没有md5校验,我怎么知道我数据的完整性,尤其 ...

  3. [R] venn.diagram保存pdf格式文件?

    vennDiagram包中的主函数绘图时,好像不直接支持PDF格式文件: dat = list(a = group_out[[1]][,1],b = group_out[[2]][,1]) names ...

  4. python-django111111111111

    111 内置电池的意思就是,内置了很多功能,插件等等帮助文档:https://docs.djangoproject.com/en/3.0/ model,很多集成的东西,连接数据库等 vierm: Te ...

  5. 小方法——匹配ip地址

    [root@MiWiFi-R1CM-srv ~]# ifconfig |sed -n '2p' inet addr:192.168.139.128 Bcast:192.168.139.255 Mask ...

  6. MariaDB——数据库登录

    登录MariaDB数据库,用root账户和密码: 显示所有数据库列表:其中,information_schema.performance_schema.test.mysql,这4个库表是数据库系统自带 ...

  7. 巩固javaweb的第二十二天

    巩固内容: 使用表单数据 : 要对用户输入的信息进行验证,需要先获取输入信息.每个表单元素都属于一个 form 表单,要获取信息,需要先获取 form,然后访问表单元素的值. 有两种方式可以获取 fo ...

  8. linux 实用指令搜索查找类

    linux 实用指令搜索查找类 目录 linux 实用指令搜索查找类 find指令 locate指令 grep指令和管道符号 | find指令 说明 从指定目录向下递归地遍历其各个子目录,将满足条件的 ...

  9. 学习java 7.18

    学习内容: Lambda表达式的格式:(形式参数)  ->  {代码块} 如果有多个参数,参数之间用逗号隔开 new Thread(  ()   ->   { System.out.pri ...

  10. 【leetcode】222. Count Complete Tree Nodes(完全二叉树)

    Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...