[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 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).html

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:less

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

Example 2:函数

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

 

Note:post

  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].

 

这道题给了咱们一个未知大小的数组,让咱们在其中搜索数字。给了咱们一个ArrayReader的类,咱们能够经过get函数来得到数组中的数字,若是越界了的话,会返回整型数最大值。既然是有序数组,又要搜索,那么二分搜索法确定是不二之选,问题是须要知道数组的首尾两端的位置,才能进行二分搜索,而这道题恰好就是大小未知的数组。因此博主的第一个想法就是先用二分搜索法来求出数组的大小,而后再用一个二分搜索来查找数字,这种方法是能够经过OJ的。但其实咱们是不用先来肯定数组的大小的,而是能够直接进行搜索数字,咱们其实是假设数组就有整型最大值个数字,在多余的位置上至关于都填上了整型最大值,那么这也是一个有序的数组,咱们能够直接用一个二分搜索法进行查找便可,参见代码以下:url

 

// Forward declaration of ArrayReader class.
class ArrayReader;

class Solution {
public:
    int search(const ArrayReader& reader, int target) {
        int left = 0, right = INT_MAX;
        while (left < right) {
            int mid = left + (right - left) / 2, x = reader.get(mid);
            if (x == target) return mid;
            else if (x < target) left = mid + 1;
            else right = mid;
        }
        return -1;
    }
};

 

相似题目:spa

Binary Searchcode

 

相似题目:htm

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/blog

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/discuss/171669/Straight-forward-binary-search.

https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size/discuss/151685/Shortest-and-cleanest-Java-solution-so-far...

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索