【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址: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:
- You may assume that all elements in the array are unique.
- 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++)的更多相关文章
- 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 ...
- [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 ...
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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. 思路 ...
- 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 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- python-django-常用models里面的Field
1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列 如果要显式的自定义一个自增列,必须设置primary_key=True. 2.mode ...
- R语言与医学统计图形-【10】ggplot2图形映射
ggplot2绘图系统--图形映射 颜色的映射. #aes中映射变量 ggplot()+geom_point(aes(x=carat,y=price,color='blue'),#color视为单一变 ...
- Django向数据库批量插入数据
# 如何向数据库一次性插入多条数据 # 方法一:效率极低,不推荐使用 for i in range(1000): models.Book.objects.create(title=f'第{i}本书') ...
- chown & chmod用法
chown & chmod 1. chown更改文件的属主&属组 NAME chown - 改变文件的属主和属组(change file owner and group) 用法 cho ...
- 1.TwoSum-Leetcode
#include<iostream> #include<algorithm> #include<map> using namespace std; class So ...
- gcc 的编译流程 和gdb的调试方法
GCC的编译流程分为四个步骤: 预处理(Pre-Processing) 编译(Compiling) 汇编(Assembling) 链接(Linking) 可以看的出来文件大小 gdb 调试 gdb - ...
- Excel-满足指定条件并且包含数字的单元格数目,DCOUNT()
DCOUNT函数 函数名称:DCOUNT 主要功能:返回数据库或列表的列中满足指定条件并且包含数字的单元格数目. 使用格式:DCOUNT(database,field,criteria) 参数说明:D ...
- java中的Arrays类
今天刚接触了数组,学到了几个比较常用的方法 Fill方法:给数组赋值 sort方法:给数组升序 equals方法:比较数组中元素 值是否相等 binarySearch方法:对排序好的数组进行二分查找法 ...
- python下载openpyxl
直接下载openpyxl报错 ERROR: Command errored out with exit status 1: python setup.py egg_info Check the log ...
- 创建Oracle数据库实例
创建Oracle数据库实例 转自oracle数据库创建实例 数据库已经安装完成,可以正常登陆查看用户等操作. system用户只能用normal身份登陆em.除非你对它授予了sysdba的系统权限或者 ...