码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 702. Search in a Sorted Array of Unknown Size

时间:2019-11-24 14:03:06      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:could   nts   https   index   integer   bounds   pos   com   turn   

原题链接在这里: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

标签:could   nts   https   index   integer   bounds   pos   com   turn   

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11921844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!