Skip to content

Commit 91942b6

Browse files
committed
Time: 30 ms (30.45%), Space: 30.2 MB (11.54%) - LeetHub
1 parent ba34bca commit 91942b6

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
int low = 0, high = nums.size() - 1;
5+
while (low <= high) {
6+
int midl = low + (high - low) / 3;
7+
int midr = high - (high - low) / 3;
8+
if (nums[midl] == target) {
9+
return midl;
10+
} else if (nums[midr] == target) {
11+
return midr;
12+
} else if (target < nums[midl]) {
13+
high = midl - 1;
14+
} else if (nums[midr] < target) {
15+
low = midr + 1;
16+
} else {
17+
low = midl + 1;
18+
high = midr - 1;
19+
}
20+
}
21+
return -1;
22+
}
23+
};

0 commit comments

Comments
 (0)