剑指Offer06 旋转数组的最小值

 1 /*************************************************************************
 2     > File Name: 06_MinNumberInRotatedArray.c
 3     > Author: Juntaran
 4     > Mail: JuntaranMail@gmail.com
 5     > Created Time: 2016年08月29日 星期一 20时14分22秒
 6  ************************************************************************/
 7 
 8 #include <stdio.h>
 9 #include <stdlib.h>
10 
11 // O(n)解法
12 int minNumberInRotatedArray1(int* rotatedArray, int length)
13 {
14     if (length <= 0)
15         return 0;
16     
17     for (int i = 1; i < length; ++i)
18     {
19         if ( rotatedArray[i] < rotatedArray[i-1])
20             return rotatedArray[i];
21     }
22     return rotatedArray[0];
23 }
24 
25 // O(log(n))解法
26 int minNumberInRotatedArray2(int* rotatedArray, int length)
27 {
28     if (length <= 0)
29         return 0;
30     if (length == 1)
31         return rotatedArray[0];
32     int left  = 0;
33     int right = length - 1;
34     int middle = 0;
35     
36     if (rotatedArray[left] < rotatedArray[right])
37         return rotatedArray[0];
38     
39     while (rotatedArray[left] >= rotatedArray[right])
40     {
41         if (right - left == 1)
42         {
43             middle = right;
44             break;
45         }
46         middle = (left + right) / 2;
47         
48         // 如果left、right、middle的值相同,只能顺序查找
49         if (rotatedArray[left]==rotatedArray[middle] 
50         && rotatedArray[middle]==rotatedArray[right])
51         {
52             for (int i = left+1; i < right; ++i)
53             {
54                 if (rotatedArray[i] < rotatedArray[i-1])
55                     return rotatedArray[i];
56             }
57             return rotatedArray[left];
58         }
59         
60         if (rotatedArray[middle] >= rotatedArray[left])
61             left = middle;
62         else if (rotatedArray[middle] <= rotatedArray[right])
63             right = middle;
64     }
65     return rotatedArray[middle];
66 }
67 
68 int main()
69 {
70     int rotatedArray1[] = {1,1,1,1,1,1,1};
71     int rotatedArray2[] = {7,8,9,9,9,1,2};
72     int rotatedArray3[] = {8,1,2,3,4,5,6};
73     int rotatedArray4[] = {8,9,1,2,8,8,8,8,8,8};
74     
75     int length1 = 7;
76     int length2 = 10;
77     
78     int ret1, ret2;
79     
80     ret1 = minNumberInRotatedArray1(rotatedArray1, length1);
81     ret2 = minNumberInRotatedArray2(rotatedArray1, length1);
82     printf("ret1 is %d, ret2 is %d\n", ret1, ret2);
83     ret1 = minNumberInRotatedArray1(rotatedArray2, length1);
84     ret2 = minNumberInRotatedArray2(rotatedArray2, length1);
85     printf("ret1 is %d, ret2 is %d\n", ret1, ret2);
86     ret1 = minNumberInRotatedArray1(rotatedArray3, length1);
87     ret2 = minNumberInRotatedArray2(rotatedArray3, length1);
88     printf("ret1 is %d, ret2 is %d\n", ret1, ret2);
89     ret1 = minNumberInRotatedArray1(rotatedArray4, length2);
90     ret2 = minNumberInRotatedArray2(rotatedArray4, length2);
91     printf("ret1 is %d, ret2 is %d\n", ret1, ret2);
92     
93     return 0;
94 }

 

posted @ 2016-08-29 20:14  Juntaran  阅读(132)  评论(0编辑  收藏  举报