LeetCode 702. Search in a Sorted Array of Unknown Size

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 702. Search in a Sorted Array of Unknown Size相关的知识,希望对你有一定的参考价值。

原题链接在这里:https://leetcode.com/problems/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).

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:

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

题解:

The range of index could not be over 20000. Because element raget is [-9999, 9999].

Guess the index using binary search, and call the reader.get() on the guess index.

If the return value cur > target, it could be either there is no such index, return is Integer.MAX_VALUE, or it exists, but value is larger. Either way, should continue guessing smaller index.

If cur < target, should continue guessing larger index.

If cur == target, return the guess index.

Time Complexity: O(logn). n = 20000.

Space:O(1).

AC Java:

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

Could use candidate call to get r. while (reader.get(r) < target). r *=2.

Then target index should be (r/2,r].

Time Complexity: O(logn).

Space: O(1).

AC Java: 

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

类似Binary Search.

以上是关于LeetCode 702. Search in a Sorted Array of Unknown Size的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 702. Search in a Sorted Array of Unknown Size

[LeetCode&Python] Problem 700. Search in a Binary Search Tree

Leetcode Search in Rotated Sorted Array

[Leetcode]700. Search in a Binary Search Tree

LeetCode | 0700. Search in a Binary Search Tree二叉搜索树中的搜索Python

LeetCode Verify Preorder Sequence in Binary Search Tree