码迷,mamicode.com
首页 > 编程语言 > 详细

6、剑指offer--旋转数组的最小数字

时间:2017-05-05 10:55:31      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:需要   vector   ota   space   off   end   return   else   题目   

题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
 
解题思路:本题中因为旋转后的两个子数组分别是有序的,因此不需要完全遍历一遍得到结果,采用二分查找的方法解决;注意特殊情况左=中间=右,只能采用遍历进行处理
 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4 //例如12345 的旋转数组34512   这类的左侧第一个值大一等于右侧   由于每个子数组是排好序的,所以用二分查找logn
 5 //例如01111 的旋转数组11101  10111这种第一个值=中间值=最后值需要特殊处理   只能遍历 n
 6 class Solution {
 7 public:
 8     int minNumberInRotateArray(vector<int> rotateArray) {
 9         int n = rotateArray.size();
10         if(n==0)
11             return 0;
12         int index1 = 0;
13         int index2 = n-1;
14         int indexMid = index1;
15         while(rotateArray[index1]>=rotateArray[index2])
16         {
17             if(index2-index1 == 1)//相差一个时,是查找截止条件
18             {
19                 indexMid = index2;
20                 break;
21             }
22             indexMid = (index1+index2)/2;
23             if(rotateArray[index1] == rotateArray[index2] && rotateArray[index1] == rotateArray[indexMid])//特殊处理
24             {
25                 return MinInOrder(rotateArray,index1,index2);
26             }
27             if(rotateArray[indexMid]>=rotateArray[index1])//中间值大于左侧值,最小值在右侧
28             {
29                 index1 = indexMid;
30             }
31             else if(rotateArray[indexMid] <=rotateArray[index2])//中间值小于右侧值最小值在左侧
32             {
33                 index2 = indexMid;
34             }
35         }
36         return rotateArray[indexMid];
37 
38     }
39     int MinInOrder(vector<int> rotateArray,int index1,int index2)
40     {
41         int result = rotateArray[index1];
42         for(int i=index1+1;i<=index2;i++)
43         {
44             if(rotateArray[i]<result)
45             {
46                 result = rotateArray[i];
47             }
48         }
49         return result;
50     }
51 };
52 int main()
53 {
54     vector<int> a;
55     a.push_back(3);
56     a.push_back(4);
57     a.push_back(5);
58     a.push_back(1);
59     a.push_back(2);
60     Solution s;
61     int result = s.minNumberInRotateArray(a);
62     cout<<"the min of 12345 is"<<result<<endl;
63 
64     vector<int> b;
65     b.push_back(1);
66     b.push_back(0);
67     b.push_back(1);
68     b.push_back(1);
69     b.push_back(1);
70     result = s.minNumberInRotateArray(b);
71     cout<<"the min of 01111 is"<<result<<endl;
72     return 0;
73 }

输出结果截图:
技术分享

 

6、剑指offer--旋转数组的最小数字

标签:需要   vector   ota   space   off   end   return   else   题目   

原文地址:http://www.cnblogs.com/qqky/p/6811466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!