返回介绍

solution / 0700-0799 / 0702.Search in a Sorted Array of Unknown Size / README_EN

发布于 2024-06-17 01:03:35 字数 4513 浏览 0 评论 0 收藏 0

702. Search in a Sorted Array of Unknown Size

中文文档

Description

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.

Solutions

Solution 1

# """
# This is ArrayReader's API interface.
# You should not implement it, or speculate about its implementation
# """
# class ArrayReader:
#  def get(self, index: int) -> int:


class Solution:
  def search(self, reader, target):
    """
    :type reader: ArrayReader
    :type target: int
    :rtype: int
    """
    left, right = 0, 20000
    while left < right:
      mid = (left + right) >> 1
      if reader.get(mid) >= target:
        right = mid
      else:
        left = mid + 1
    return left if reader.get(left) == target else -1
/**
 * // This is ArrayReader's API interface.
 * // You should not implement it, or speculate about its implementation
 * interface ArrayReader {
 *   public int get(int index) {}
 * }
 */

class Solution {
  public int search(ArrayReader reader, int target) {
    int left = 0, right = 20000;
    while (left < right) {
      int mid = left + right >> 1;
      if (reader.get(mid) >= target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return reader.get(left) == target ? left : -1;
  }
}
/**
 * // This is the ArrayReader's API interface.
 * // You should not implement it, or speculate about its implementation
 * class ArrayReader {
 *   public:
 *   int get(int index);
 * };
 */

class Solution {
public:
  int search(const ArrayReader& reader, int target) {
    int left = 0, right = 20000;
    while (left < right) {
      int mid = left + right >> 1;
      if (reader.get(mid) >= target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return reader.get(left) == target ? left : -1;
  }
};
/**
 * // This is the ArrayReader's API interface.
 * // You should not implement it, or speculate about its implementation
 * type ArrayReader struct {
 * }
 *
 * func (this *ArrayReader) get(index int) int {}
 */

func search(reader ArrayReader, target int) int {
  left, right := 0, 20000
  for left < right {
    mid := (left + right) >> 1
    if reader.get(mid) >= target {
      right = mid
    } else {
      left = mid + 1
    }
  }
  if reader.get(left) == target {
    return left
  }
  return -1
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文