-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path704-binary-search.cpp
35 lines (31 loc) · 1.15 KB
/
704-binary-search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Title: Binary Search
// Description:
// Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums.
// If target exists, then return its index. Otherwise, return -1.
// You must write an algorithm with O(log n) runtime complexity.
// Link: https://leetcode.com/problems/binary-search/
// Time complexity: O(log(n))
// Space complexity: O(1)
class Solution {
public:
int search(std::vector<int> &nums, int target) {
std::size_t begin = 0, end = nums.size();
// keep searching if the interval is not empty
while (begin != end) {
// locate the middle index
std::size_t mid = begin + (end - begin) / 2;
if (nums[mid] < target) {
// eliminate the smaller half
begin = mid + 1;
} else if (nums[mid] > target) {
// eliminate the bigger half
end = mid;
} else {
// the target is found
return mid;
}
}
// the target is not found
return -1;
}
};