zoukankan      html  css  js  c++  java
  • [LeetCode] 702. Search in a Sorted Array of Unknown Size

    This is an interactive problem.

    You have a sorted array of unique elements and an unknown size. You do not have an access to the array but you can use the ArrayReader interface to access it. You can call ArrayReader.get(i) that:

    • returns the value at the ith index (0-indexed) of the secret array (i.e., secret[i]), or
    • returns 231 - 1 if the i is out of the boundary of the array.

    You are also given an integer target.

    Return the index k of the hidden array where secret[k] == target or return -1 otherwise.

    You must write an algorithm with O(log n) runtime complexity.

    Example 1:

    Input: secret = [-1,0,3,5,9,12], target = 9
    Output: 4
    Explanation: 9 exists in secret and its index is 4.
    

    Example 2:

    Input: secret = [-1,0,3,5,9,12], target = 2
    Output: -1
    Explanation: 2 does not exist in secret so return -1.

    Constraints:

    • 1 <= secret.length <= 104
    • -104 <= secret[i], target <= 104
    • secret is sorted in a strictly increasing order.

    在未知大小的有序数组中搜索。

    题干即是题意,给一个有序数组但是不告知数组长度,请找一个 target 数字。

    思路是用二分法。先设一个变量 high,因为不确定 input 数组的长度所以只能每次乘以二去试探 high 位置上的数字和 target 数字的大小关系。

    时间O(logn)

    空间O(1)

    JS实现

     1 /**
     2  * @param {ArrayReader} reader
     3  * @param {number} target
     4  * @return {number}
     5  */
     6 var search = function (reader, target) {
     7     let hi = 1;
     8     while (reader.get(hi) < target) {
     9         hi = hi << 1;
    10     }
    11     let low = hi >> 1;
    12     while (low <= hi) {
    13         let mid = Math.floor(low + (hi - low) / 2);
    14         if (reader.get(mid) > target) {
    15             hi = mid - 1;
    16         } else if (reader.get(mid) < target) {
    17             low = mid + 1;
    18         } else {
    19             return mid;
    20         }
    21     }
    22     return -1;
    23 };

    Java实现

     1 class Solution {
     2     public int search(ArrayReader reader, int target) {
     3         int hi = 1;
     4         while (reader.get(hi) < target) {
     5             hi = hi << 1;
     6         }
     7         int low = hi >> 1;
     8         while (low <= hi) {
     9             int mid = low + (hi - low) / 2;
    10             if (reader.get(mid) > target) {
    11                 hi = mid - 1;
    12             } else if (reader.get(mid) < target) {
    13                 low = mid + 1;
    14             } else {
    15                 return mid;
    16             }
    17         }
    18         return -1;
    19     }
    20 }

    LeetCode 题目总结

  • 相关阅读:
    "Automation 服务器不能创建对象" 的解决方法
    让DataGrid拥有单击回传事件并带回指定字段的值
    SQL 和TSQL学习(一)
    数据写入XML
    DATALIST分页存储过程
    The target assembly contains no service types. You may need to adjust the Code Access Security policy of this assembly." 目标程序集不包含服务类型。可能需要调整此程序集的代码访问
    字符串转日期
    C# 日期和时间正则表达式
    js根据输入日期显示该月的最后一天的日期[转]
    JavaScript试题【解析+答案】
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12442836.html
Copyright © 2011-2022 走看看