Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec76554

Browse files
solves #702: Search in sorted array of unknown size in java
1 parent e57910c commit ec76554

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
| 696 | [Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings) | [![Java](assets/java.png)](src/CountBinarySubstrings.java) [![Python](assets/python.png)](python/count_binary_substrings.py) | |
399399
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array) | [![Java](assets/java.png)](src/DegreeOfAnArray.java) [![Python](assets/python.png)](python/degree_of_an_array.py) | |
400400
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree) | [![Java](assets/java.png)](src/SearchInBinarySearchTree.java) [![Python](assets/python.png)](python/search_in_binary_search_tree.py) | |
401+
| 702 | [Search In A Sorted Array of Unknown Size](https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size) | [![Java](assets/java.png)](src/SearchInSortedArrayOfUnknownSize.java) | |
401402
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream) | [![Java](assets/java.png)](src/KthLargestElementInAStream.java) [![Python](assets/python.png)](python/k_th_largest_element_in_a_stream.py) | |
402403
| 704 | [Binary Search](https://leetcode.com/problems/binary-search) | [![Java](assets/java.png)](src/BinarySearch.java) [![Python](assets/python.png)](python/binary_search.py) | |
403404
| 705 | [Design HashSet](https://leetcode.com/problems/design-hashset) | [![Java](assets/java.png)](src/DesignHashSet.java) [![Python](assets/python.png)](python/design_hash_set.py) | |
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size
2+
// T: O(logN)
3+
// S: O(1)
4+
5+
public class SearchInSortedArrayOfUnknownSize {
6+
public interface ArrayReader {
7+
int get(int index);
8+
}
9+
10+
public int search(ArrayReader reader, int target) {
11+
int left = 0, right = 1, middle;
12+
13+
while (reader.get(right) < target) {
14+
left = right;
15+
right *= 2;
16+
}
17+
18+
while (left <= right) {
19+
middle = left + (right - left) / 2;
20+
if (reader.get(middle) == target) return middle;
21+
else if (reader.get(middle) < target) left = middle + 1;
22+
else right = middle - 1;
23+
}
24+
return -1;
25+
}
26+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /