|
35 | 35 | <li>The length of the array will be in the range <code>[1, 10^4]</code>.</li>
|
36 | 36 | </ul>
|
37 | 37 |
|
38 |
| - |
39 | 38 | ## Solutions
|
40 | 39 |
|
41 | 40 | <!-- tabs:start -->
|
42 | 41 |
|
43 | 42 | ### **Python3**
|
44 | 43 |
|
45 | 44 | ```python
|
46 |
| - |
| 45 | +# """ |
| 46 | +# This is ArrayReader's API interface. |
| 47 | +# You should not implement it, or speculate about its implementation |
| 48 | +# """ |
| 49 | +#class ArrayReader: |
| 50 | +# def get(self, index: int) -> int: |
| 51 | + |
| 52 | +class Solution: |
| 53 | + def search(self, reader, target): |
| 54 | + """ |
| 55 | + :type reader: ArrayReader |
| 56 | + :type target: int |
| 57 | + :rtype: int |
| 58 | + """ |
| 59 | + left, right = 0, 20000 |
| 60 | + while left < right: |
| 61 | + mid = (left + right) >> 1 |
| 62 | + if reader.get(mid) >= target: |
| 63 | + right = mid |
| 64 | + else: |
| 65 | + left = mid + 1 |
| 66 | + return left if reader.get(left) == target else -1 |
47 | 67 | ```
|
48 | 68 |
|
49 | 69 | ### **Java**
|
50 | 70 |
|
51 | 71 | ```java
|
| 72 | +/** |
| 73 | + * // This is ArrayReader's API interface. |
| 74 | + * // You should not implement it, or speculate about its implementation |
| 75 | + * interface ArrayReader { |
| 76 | + * public int get(int index) {} |
| 77 | + * } |
| 78 | + */ |
| 79 | + |
| 80 | +class Solution { |
| 81 | + public int search(ArrayReader reader, int target) { |
| 82 | + int left = 0, right = 20000; |
| 83 | + while (left < right) { |
| 84 | + int mid = left + right >> 1; |
| 85 | + if (reader.get(mid) >= target) { |
| 86 | + right = mid; |
| 87 | + } else { |
| 88 | + left = mid + 1; |
| 89 | + } |
| 90 | + } |
| 91 | + return reader.get(left) == target ? left : -1; |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### **C++** |
| 97 | + |
| 98 | +```cpp |
| 99 | +/** |
| 100 | + * // This is the ArrayReader's API interface. |
| 101 | + * // You should not implement it, or speculate about its implementation |
| 102 | + * class ArrayReader { |
| 103 | + * public: |
| 104 | + * int get(int index); |
| 105 | + * }; |
| 106 | + */ |
| 107 | + |
| 108 | +class Solution { |
| 109 | +public: |
| 110 | + int search(const ArrayReader& reader, int target) { |
| 111 | + int left = 0, right = 20000; |
| 112 | + while (left < right) { |
| 113 | + int mid = left + right >> 1; |
| 114 | + if (reader.get(mid) >= target) { |
| 115 | + right = mid; |
| 116 | + } else { |
| 117 | + left = mid + 1; |
| 118 | + } |
| 119 | + } |
| 120 | + return reader.get(left) == target ? left : -1; |
| 121 | + } |
| 122 | +}; |
| 123 | +``` |
52 | 124 |
|
| 125 | +### **Go** |
| 126 | +
|
| 127 | +```go |
| 128 | +/** |
| 129 | + * // This is the ArrayReader's API interface. |
| 130 | + * // You should not implement it, or speculate about its implementation |
| 131 | + * type ArrayReader struct { |
| 132 | + * } |
| 133 | + * |
| 134 | + * func (this *ArrayReader) get(index int) int {} |
| 135 | + */ |
| 136 | +
|
| 137 | +func search(reader ArrayReader, target int) int { |
| 138 | + left, right := 0, 20000 |
| 139 | + for left < right { |
| 140 | + mid := (left + right) >> 1 |
| 141 | + if reader.get(mid) >= target { |
| 142 | + right = mid |
| 143 | + } else { |
| 144 | + left = mid + 1 |
| 145 | + } |
| 146 | + } |
| 147 | + if reader.get(left) == target { |
| 148 | + return left |
| 149 | + } |
| 150 | + return -1 |
| 151 | +} |
53 | 152 | ```
|
54 | 153 |
|
55 | 154 | ### **...**
|
|
0 commit comments