-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path704.cpp
More file actions
53 lines (45 loc) · 1.42 KB
/
704.cpp
File metadata and controls
53 lines (45 loc) · 1.42 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
48
49
50
51
52
53
__________________________________________________________________________________________________
sample 16 ms submission
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
class Solution {
public:
int search(vector<int>& nums, int target) {
int a = 0, b = nums.size();
if (b == 0) {
return -1;
}
while (true) {
int m = (a + b) >> 1 ; // ((b - a) >> 1) + a;
if (a == m) {
return nums[a] == target ? a : -1;
}
if (nums[m] > target) {
b = m;
} else {
a = m;
}
}
}
};
__________________________________________________________________________________________________
sample 10700 kb submission
class Solution {
public:
int search(vector<int>& nums, int target) {
int r = -1, i = 0, j = nums.size() - 1;
int m = (j - i) >> 1;
while (i <= j) {
if (nums[m] == target) return m;
if (target > nums[m]) {
i = ++m;
}
else {
j = --m;
}
m = ((j - i) >> 1) + i;
}
return r;
}
};
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
__________________________________________________________________________________________________