跳转至

702. 搜索长度未知的有序数组 🔒

题目描述

这是一个交互问题

您有一个升序整数数组,其长度未知。您没有访问数组的权限,但是可以使用 ArrayReader 接口访问它。你可以调用 ArrayReader.get(i):

  • 返回数组第ith个索引(0-indexed)处的值(即 secret[i]),或者

  • 如果 i  超出了数组的边界,则返回 231 - 1

你也会得到一个整数 target

如果存在secret[k] == target,请返回索引 k 的值;否则返回 -1

你必须写一个时间复杂度为 O(log n) 的算法。

 

示例 1:

输入: secret = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 存在在 nums 中,下标为 4

示例 2:

输入: secret = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不在数组中所以返回 -1

 

提示:

  • 1 <= secret.length <= 104
  • -104 <= secret[i], target <= 104
  • secret 严格递增

解法

方法一:二分查找

我们先定义一个指针 $r = 1$,每一次判断 $r$ 处的值是否小于目标值,如果小于目标值,我们将 $r$ 乘以 $2$,即左移一位,直到 $r$ 处的值大于等于目标值。此时,我们可以确定目标值在 $[r / 2, r]$ 的区间内。

接下来,我们定义一个指针 $l = r / 2$,然后我们可以使用二分查找的方法在 $[l, r]$ 的区间内查找目标值的位置。

时间复杂度 $O(\log M)$,其中 $M$ 为目标值的位置。空间复杂度 $O(1)$。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
# """
# 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: "ArrayReader", target: int) -> int:
        r = 1
        while reader.get(r) < target:
            r <<= 1
        l = r >> 1
        while l < r:
            mid = (l + r) >> 1
            if reader.get(mid) >= target:
                r = mid
            else:
                l = mid + 1
        return l if reader.get(l) == target else -1
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * // 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 r = 1;
        while (reader.get(r) < target) {
            r <<= 1;
        }
        int l = r >> 1;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (reader.get(mid) >= target) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return reader.get(l) == target ? l : -1;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * // 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 r = 1;
        while (reader.get(r) < target) {
            r <<= 1;
        }
        int l = r >> 1;
        while (l < r) {
            int mid = (l + r) >> 1;
            if (reader.get(mid) >= target) {
                r = mid;
            } else {
                l = mid + 1;
            }
        }
        return reader.get(l) == target ? l : -1;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * // 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 {
    r := 1
    for reader.get(r) < target {
        r <<= 1
    }
    l := r >> 1
    for l < r {
        mid := (l + r) >> 1
        if reader.get(mid) >= target {
            r = mid
        } else {
            l = mid + 1
        }
    }
    if reader.get(l) == target {
        return l
    }
    return -1
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * class ArrayReader {
 *      // This is the ArrayReader's API interface.
 *      // You should not implement it, or speculate about its implementation
 *      get(index: number): number {};
 *  };
 */

function search(reader: ArrayReader, target: number): number {
    let r = 1;
    while (reader.get(r) < target) {
        r <<= 1;
    }
    let l = r >> 1;
    while (l < r) {
        const mid = (l + r) >> 1;
        if (reader.get(mid) >= target) {
            r = mid;
        } else {
            l = mid + 1;
        }
    }
    return reader.get(l) === target ? l : -1;
}

评论