-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearch.cpp
More file actions
47 lines (39 loc) · 1.11 KB
/
binarySearch.cpp
File metadata and controls
47 lines (39 loc) · 1.11 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <cmath>
// coins = [[-1,0,3,5,9,12], target = 9
// https://leetcode.com/problems/binary-search
class Solution
{
public:
int search(std::vector<int>& nums, int target)
{
return _innerSearch(nums, target, 0, nums.size() - 1);
}
int _innerSearch(std::vector<int>& nums, int target, int startIndex, int endIndex) {
int range = endIndex - startIndex + 1;
if (range <= 2) {
if (nums[startIndex] == target) return startIndex;
if (nums[endIndex] == target) return endIndex;
return -1;
}
int middleIndex = std::floor((endIndex + startIndex) / 2);
int nextStartIndex = startIndex;
int nextEndIndex = endIndex;
if (nums[middleIndex] < target) {
nextStartIndex = middleIndex + 1;
} else {
nextEndIndex = middleIndex;
}
return _innerSearch(nums, target, nextStartIndex, nextEndIndex);
}
};
int main()
{
Solution SolutionInstance;
std::vector<int> nums = {-1, 0,3,5,9,12};
int target = 9;
int result = SolutionInstance.search(nums, target);
std::cout << "result is: " << result << std::endl;
return 0;
}