原题链接在这里:https://leetcode.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].

题解:

The range of index could not be over 20000. Because element raget is [-9999, 9999].

Guess the index using binary search, and call the reader.get() on the guess index.

If the return value cur > target, it could be either there is no such index, return is Integer.MAX_VALUE, or it exists, but value is larger. Either way, should continue guessing smaller index.

If cur < target, should continue guessing larger index.

If cur == target, return the guess index.

Time Complexity: O(logn). n = 20000.

Space:O(1).

AC Java:

 class Solution {
public int search(ArrayReader reader, int target) {
int l = 0;
int r = 20000;
while(l <= r){
int mid = l + (r-l)/2;
int cur = reader.get(mid);
if(cur > target){
r = mid-1;
}else if(cur < target){
l = mid+1;
}else{
return mid;
}
} return -1;
}
}

Could use candidate call to get r. while (reader.get(r) < target). r *=2.

Then target index should be (r/2,r].

Time Complexity: O(logn).

Space: O(1).

AC Java:

 class Solution {
public int search(ArrayReader reader, int target) {
int r = 1;
while(reader.get(r) < target){
r = r << 1;
} int l = r >> 1;
while(l <= r){
int mid = l + (r-l)/2;
int cur = reader.get(mid);
if(cur > target){
r = mid-1;
}else if(cur < target){
l = mid+1;
}else{
return mid;
}
} return -1;
}
}

类似Binary Search.

LeetCode 702. 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] 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. 大数据技术 - 为什么是SQL

    在大数据处理以及分析中 SQL 的普及率非常高,几乎是每一个大数据工程师必须掌握的语言,甚至非数据处理岗位的人也在学习使用 SQL.今天这篇文章就聊聊 SQL 在数据分析中作用以及掌握 SQL 的必要 ...

  2. 浅浅的叙WPF之数据驱动与命令

    之前一直开发Winfrom程序,由于近一段时间转开发Wpf程序,刚好拜读刘铁锰<深入浅出WPF>对此有一些理解,如有误导指出,还望斧正!!! 说道WPF数据驱动的编程思想,MVVM,是为W ...

  3. 解决WPF下popup不随着window一起移动的问题

    /// <summary> /// Popup帮助类,解决Popup设置StayOpen="True"时,移动窗体或者改变窗体大小时,Popup不随窗体移动的问题 // ...

  4. Micro 设计文档

    1.顶部 即时通讯 | 应用软件 | 网络学院 | 帮助中心 | 开发者中心 | 控制台 2.导航 首页 免费制作 免费下载 功能介绍 示例演示 云数据 支持与服务 免费制作:https://www. ...

  5. ASP.NET SignalR 系列(八)之跨域推送

    前面几章讲的都是同域下的推送和订阅.这种讲讲如何跨域 对于SignalR来说,默认是不允许跨域的,因为安全问题.虽如此,但同时提供了跨域方案. 两种跨域方式: 1:JSONP2:CORS JSONP的 ...

  6. python3对字符串进行base64转码

    import base64# 使用base64的b64encode()进行转码,转码之后在用‘utf-8’解码# s 要转码的字符串res = base64.b64encode(s.encode(&q ...

  7. JavaScript---js语法,数据类型及方法, 数组及方法,JSON对象及方法,日期Date及方法,正则及方法,数据类型转换,运算符, 控制流程(三元运算),函数(匿名函数,自调用函数)

    day46 一丶javascript介绍 JavaScript的基础分为三个       1.ECMAScript:JavaScript的语法标准.包括变量,表达式,运算符,函数,if语句,for语句 ...

  8. python的new与init

    基于文章:Why is init() always called after new()? 特别说明: 这篇文章的灵感来源于stackoverflow的一个提问,准确说,我只是做了些知识梳理的工作,对 ...

  9. Vue3.0报错error: Unexpected console statement (no-console) 解决办法

    写项目过程中用ESLint遵守代码规范很有必要,但是对于一些规范也很是无语,比如:‘Unexpected console statement (no-console)’,连console都不能用,这就 ...

  10. js如何保留两位小数,并进行四舍五入

    保留两位小数,并进行四舍五入使用js函数 toFixed() 函数传递一个参数(Number) Number就为需要保留小数的位数 具体实现代码 <script language="j ...