-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
1064.cpp
31 lines (31 loc) · 887 Bytes
/
1064.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
__________________________________________________________________________________________________
sample 16 ms submission
class Solution {
public:
int fixedPoint(vector<int>& A) {
for(int i = 0;i < A.size();++i) {
if(A[i] == i) {
return i;
}
}
return -1;
}
};
__________________________________________________________________________________________________
class Solution {
public:
int fixedPoint(vector<int>& A) {
int l = 0, r = A.size() - 1;
while (l <= r) {
int m = (l + r) / 2;
if (A[m] - m > 0)
r = m - 1;
else if (A[m] - m < 0)
l = m + 1;
else
return m;
}
return -1;
}
};
__________________________________________________________________________________________________