首页 > 其他分享 >702-search-in-a-sorted-array-of-unknown-size

702-search-in-a-sorted-array-of-unknown-size

时间:2022-08-22 11:16:42浏览次数:60  
标签:search right target val unknown 702 array size

Given an integer array sorted in ascending order, write a function to searchtargetinnums. Iftargetexists, then return its index, otherwise return-1.However, the array size is unknown to you. You may only access the array using anArrayReader interface, where ArrayReader.get(k)returns the element of the array at indexk (0-indexed).

You may assume all integers in the array are less than 10000, and if you access the array out of bounds,ArrayReader.getwill return2147483647.

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].
public int search(ArrayReader reader, int target) {
        int right = 0, val = reader.get(right);
        while (val != Integer.MAX_VALUE) {
            if (val < target) {
                right = right == 0 ? 1 : right * 2;
                val = reader.get(right);
            } else {
                break;
            }
        }
        
        int left = right / 2, mid;
        while (left <= right) {
            mid = left + (right - left) / 2;
            val = reader.get(mid);
            if (val == target) {
                return mid;
            } else if (val < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

what if we don't know the size of array(the search range)?

we will find an approximate one, by double the right index every time untile arr[right] > target

Then do a regular bs

标签:search,right,target,val,unknown,702,array,size
From: https://www.cnblogs.com/wentiliangkaihua/p/16612139.html

相关文章

  • Elasticsearch基本操作
    索引操作创建索引PUT/user查询指定索引GET/user查询所有索引信息GET_cat/indices?v删除索引DELETE/user类型映射自动映射elasticsearch默认情况下会自......
  • Elasticsearch Java client使用
    前言Elasticsearch官方列出了好几个客户端,如下所示JavaClientJavaRestClient(JavaHighLevelRESTClient)JavaTransportClient其中JavaRestClient在7.15.0被......
  • Elasticsearch 实战
    需求假设现在有这么一个需求,系统接了很多的报文,需要提供全文检索,为了简化,报文目前只有类型,流水号,内容这三个字段。索引设计建立msg索引,映射规则如下PUT/msg{ "mappi......
  • Elasticsearch学习环境搭建
    Elasticsearch安装官方文档下载windows7.17.5版本安装包,安装包是一个zip,和tomcat一样解压即可用,elasticsearch依赖JDK环境,至少需要JDK1.8版本。运行#进入bin目录......
  • elaticsearch
                  扩展词典                                    ......
  • PHP正则的Unknown Modifier错误解决方法
    https://www.jb51.net/article/22304.htm 如下正则:$a='2<span><nobr>tóng<spanclass="h">dòng</span></nobr><br>垌</span>3';echopreg_replace('/<spanclas......
  • 学习Depth First Search和Breadth First Search
    目录HerewelearnDFSandBFSinpython......
  • Docker创建ElasticSearch集群以及常见问题
    本篇文章演示在WSL2中通过DockerCompose搭建ES集群,解决其中遇到的问题。虚拟内存最大映射数第一个问题是几乎所有机器上搭建ES集群都会遇到的,即虚拟内存最大映射数默认为......
  • elasticsearch中使用curl进行的简单查询
    curl:-X:指定http的请求方式,有HEAD、GET、POST、PUT、DELETE-d:指定要传输的数据-H:指定http的请求头信息curl-XPUThttp://ip:port/索引名?pretty--创建索引cur......
  • ElasticSearch Java 客户端请求超时
    版本jdk:1.8elasticsearch:5.6.16elasticsearch-rest-high-level-client:5.6.16异常java.io.IOException:listenertimeoutafterwaitingfor[30000]ms atorg.el......