-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFind_Minimum_in_Rotated_Sorted_Array.cpp
More file actions
74 lines (66 loc) · 1.28 KB
/
Find_Minimum_in_Rotated_Sorted_Array.cpp
File metadata and controls
74 lines (66 loc) · 1.28 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Source : https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/
// Author : zheng yi xiong
// Date : 2014-11-6
/**********************************************************************************
*
* Suppose a sorted array is rotated at some pivot unknown to you beforehand.
* (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
* Find the minimum element.
* You may assume no duplicate exists in the array.
*
**********************************************************************************/
class Solution {
public:
int findMin(vector<int> &num) {
int big = num.size(); //head or tail where is large
if (0 == big)
{
return 0;
}
else if (1 == big)
{
return num[0];
}
else if (2 == big)
{
if (num[0] < num[1])
{
return num[0];
}
else
{
return num[1];
}
}
big = big - 1;
int little = 0; //head or tail where is little
if (num[little] > num[big])
{
little = big;
big = 0;
}
int mid = big / 2;
int newMid = mid;
do
{
mid = newMid;
if (num[mid] < num[little])
{
little = mid;
}
else
{
big = mid;
}
newMid = (little + big) / 2;
} while (newMid != mid);
if (num[mid] < num[little])
{
return num[mid];
}
else
{
return num[little];
}
}
};