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 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 innumsand its index is 4
Example 2:
Input:array= [-1,0,3,5,9,12],target= 2
Output: -1
Explanation: 2 does not exist innumsso 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].
题解:
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;
}
}
LeetCode 702. Search in a Sorted Array of Unknown Size的更多相关文章
- 【LeetCode】702. Search in a Sorted Array of Unknown Size 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 二分查找 日期 题目地址:https://lee ...
- [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 ...
随机推荐
- 解决:Java source1.6不支持diamond运算符,请使用source 7或更高版本以启用diamond运算符
diamond运算符,指的是JDK1.7的一个新特性.Maven默认用的是JDK1.6去编译.所以需要配置成更高的版本,有以下几种解决办法: 1.在项目pom.xml中加入下面的配置即可 <pr ...
- 微信小程序之使用函数防抖与函数节流
函数防抖和函数节流都是老生常谈的问题了.这两种方式都能优化 js 的性能.有些人可能会搞混两个的概念.所以,我以自己的理解,来解释这两个概念的含义.并且列举在小程序中这两个方法的使用. 函数防抖: 英 ...
- GoF的23种设计模式之创建型模式的特点和分类
创建型模式的主要关注点是“怎样创建对象?”,它的主要特点是“将对象的创建与使用分离”.这样可以降低系统的耦合度,使用者不需要关注对象的创建细节,对象的创建由相关的工厂来完成.就像我们去商场购买商品时, ...
- TreeView树,全选,反选,平级选操作
首先事件选择,选择的是MouseUp事件.为啥?因为凡是跟Check有关的,在选中父节点或者子节点,都会二次触发.然后发生的就是死循环. Up事件就可以避免二次触发.Down事件呢?那就触发After ...
- java面试经常涉及到的
需要掌握的Java知识点: 1 基本数据类型.循环控制.String类型的使用.数组.类和对象.接口和抽象类.面向对象三大特征.异常处理.集合类(List.Map.Set) 2 能够熟练使用Sprin ...
- Mybatis 复习
概述 mybatis 是一个用java编写的持久层框架, 它封装了jdbc操作的很多细节,使开发者只需要关注sql语句本身,而无需关注注册驱动,创建连接等繁杂过程,它使用了ORM思想实现了结果 集的封 ...
- spark存储模块之内存存储--MemeoryStore
MemeoryStore 上一节,我们对BlockManager的主要写入方法做了一个整理,知道了BlockMananger的主要写入逻辑,以及对于块信息的管理.但是,由于spark的整个存储模块是在 ...
- CSS文本单行或者多行超出区域省略号(...)显示方法
单行超出时,主要用到几个CSS属性: 1.text-overflow : clip | ellipsis ; clip : 不显示省略标记(...),而是简单的裁切ellipsis : 当对象内文本溢 ...
- Java 数组(二)基本操作
一.数组的基本操作 1.数组遍历[重点] 数组遍历:就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 方式一:使用索引下标方式 int[] array = { 15, 25, ...
- ES10(2019)有哪些更新和新特性?
ES10新特性(2019) 行分隔符(U + 2028)和段分隔符(U + 2029)符号现在允许在字符串文字中,与JSON匹配 更加友好的 JSON.stringify 新增了Array的flat( ...