题目地址: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:

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

题目大意

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

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

解题方法

遍历

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

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

C++代码如下:

// Forward declaration of ArrayReader class.
class ArrayReader; class Solution {
public:
int search(const ArrayReader& reader, int target) {
int index = 0;
while (reader.get(index) != 2147483647) {
if (reader.get(index) == target)
return index;
index ++;
}
return -1;
}
};

二分查找

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

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

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

C++代码如下:

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

日期

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. mount 挂载详解

    挂接命令(mount) 首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的. 命令格式:mount [-t vfstype] [-o option ...

  2. nginx_日志切割脚本

    #!/bin/bash NGINX_LOG=/usr/loca/nginx/logs/access.log RE_LOG=/data/backup/`data +%Y%m%d` echo -e &qu ...

  3. 在VS2008环境下编写C语言DLL,并在C++和C#项目下调用 (转载)

    1.编写DLL a)文件--打开--新建项目--Win32,右侧Win32项目,填写好项目名称,点击"下一步", 应用程序类型选择:"DLL(D)",附加选项: ...

  4. Swift-技巧(十一)重写运算符

    摘要 基础数据的运算可以直接使用四则运算符.在 Swift 中也可以通过重写四则运算符的方式,让 struct 或者 class 创建的结构体或者对象也能像基础数据那样直接使用四则运算符. Swift ...

  5. Fllin(七)【Flink CDC实践】

    目录 FlinkCDC 1.简介 2.依赖 3.flink stream api 4.flink sql 5.自定义反序列化器 6.打包测试 FlinkCDC 1.简介 CDC是Change Data ...

  6. 爬虫系列:连接网站与解析 HTML

    这篇文章是爬虫系列第三期,讲解使用 Python 连接到网站,并使用 BeautifulSoup 解析 HTML 页面. 在 Python 中我们使用 requests 库来访问目标网站,使用 Bea ...

  7. git 的基本流程

    有个本地文件 打开 新建一个 打开git $ git push origin master 这里是上传文件.  (你每次上传的时候,都要先提交到本地的仓库...然后再上传) github上就有了 如何 ...

  8. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

  9. 【Services】【Web】【tomcat】配置tomcat支持https传输

    1. 基础: 1.1. 描述:内网的tomcat接到外网nginx转发过来的请求之后需要和外网的客户端进行通讯,为了保证通讯内容的安装,使用tomcat使用https协议. 1.2. 链接:http: ...

  10. mysq中char,varchar,text的区别

    mysql5.0.3以后,n都表示字符数(varchar(n)) 检索效率 char > varchar > text 当varchar长度超过255之后,跟text一致,但是设置varc ...