-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_MinNumberInRotatedArray.cpp
More file actions
67 lines (52 loc) · 1.25 KB
/
Copy path8_MinNumberInRotatedArray.cpp
File metadata and controls
67 lines (52 loc) · 1.25 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
#include <iostream>
#include <stdexcept>
using namespace std;
int MinInorder(int* nums, int low, int high)
{
int result = nums[low];
for (int i = low+1; i <= high; i++)
{
if (nums[i] < result)
result = nums[i];
}
return result;
}
int Min(int* nums, int length)
{
if (nums == NULL || length <= 0)
throw new runtime_error("argument invalid");
int low = 0, high = length - 1;
int m = 0;
while (nums[low] >= nums[high])
{
if (high - low == 1)
{
m = high;
break;
}
int m = (low + high) / 2;
if (nums[low] == nums[m] && nums[high] == nums[m])
return MinInorder(nums, low, high);
else if (nums[m] >= nums[low])
low = m;
else
high = m;
}
return nums[m];
}
int main()
{
int nums1[] = {3,4,5,1,2};
cout << Min(nums1, 5) << endl;
int nums2[] = {4,5,1,2,3};
cout << Min(nums2, 5) << endl;
int nums3[] = {5,1,2,3,4};
cout <<Min(nums3, 5) << endl;
int nums4[] = {1,2,3,4,5};
cout << Min(nums4, 5) << endl;
int nums5[] = {1,0,1,1,1};
cout << Min(nums5, 5) << endl;
int nums6[] = {1,1,1,0,1};
cout << Min(nums5, 5) << endl;
return 0;
}