-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
40 lines (36 loc) · 848 Bytes
/
solution.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
36
37
38
39
40
class Solution
{
public:
int findMin(vector<int> &num)
{
if(num.size() == 1)
return num[0];
int midPos = num.size()/2;
int left = 0;
int right = num.size()-1;
int minEle = num[0];
while(left < midPos && midPos < right)
{
if(num[left] > num[midPos])
{
right = midPos;
midPos = (left+right)/2;
}
else if(num[midPos] > num[right])
{
left = midPos;
midPos = (left+right)/2;
}
else
{
minEle = num[left];
break;
}
}
if(num[left] < num[right])
minEle = num[left];
else
minEle = num[right];
return minEle;
}
};