-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_704.h
53 lines (47 loc) · 1.51 KB
/
binary_search_704.h
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
48
49
50
51
52
53
//
// Created by Jing Liu on 26.10.23.
//
#ifndef PROGRAMMERCARL_CPP_BINARY_SEARCH_704_H
#define PROGRAMMERCARL_CPP_BINARY_SEARCH_704_H
#include "iostream"
#include "vector"
using namespace std;
// 左闭右闭法
class Solution_1 {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1; // index starts from 0
while (left <= right) {
int middle = left + (right - left) / 2; // avoid integer overflow
if (target < nums[middle]) { // cannot include equal sign
right = middle - 1; // target is in the left interval of array (left side of the middle point) --> target in [left, middle - 1]
} else if (target > nums[middle]) { // cannot include equal sign
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
};
// 左闭右开法
class Solution_2 {
public:
int search(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while (left < right) { // can not include equal sign. when left == right, the interval [left,right) is invalid
int middle = left + (right - left) / 2; //
if (target < nums[middle]) {
right = middle; //
} else if (target > nums[middle]) {
left = middle + 1;
} else {
return middle;
}
}
return -1;
}
};
#endif //PROGRAMMERCARL_CPP_BINARY_SEARCH_704_H