【剑指Offer】面试题8:旋转数组的最小数字
2017-07-06 16:49
507 查看
一:题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
二:解题思路
非递减排列 :数字按照增加的顺序排列,其中有相等的数
如:
1,2,3,4,5,.:递增排列
9,8,7,6,5.:递减排列
1,2,3,3,4,5,8,8,.:非递减排列
9,8,7,7,6,5,5,2,1,.:非递增排列
题目中把排序数组的前面n个元素搬到最后面,可能n=0,要注意
第一个简单的想法:
旋转后的数组实际上可以划分为两个排序数组,而且前面的子数组的元素都大于等于后面子数组的元素,而最小元素刚好是这两个子数组的分界线
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0)
return 0;
if(rotateArray.size()==1)
return rotateArray[0];
int length=rotateArray.size();
int i;
for(i=0;i<=length-2;i++)
if(rotateArray[i]>rotateArray[i+1])
return rotateArray[i+1];
//如果数组转换后,是一个非递减排序,第一位是最小的
return rotateArray[0];
}
};
有时不要把题看的太简单,往往都是出题者的坑,套路,就算实现了,可能也不是最佳情况,剑指Offer给出的最佳结果是基于排序的思想
用两个指针分别指向两个子数组,第一个指针总是指向前面递增数组的元素,第二个指针总是指向后面递增的数组的元素,最终第一个指针指向前面子数组的最后一个元素,第二个指针指向后面数组的第一个元素,他们最终指向两个相邻的元素,而第二个指针刚好指向最小的元素。
特殊情况
当第一个指针,中间位置,与第二个指针所指元素相同的时候怎么办呢?采用顺序查找
三:代码实现
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0)
return 0;
if(rotateArray.size()==1)
return rotateArray[0];
int index1=0;
int index2=rotateArray.size()-1;
int midIndex=index1;
while(rotateArray[index1]>=rotateArray[index2]){
if(index2-index1==1){
midIndex=index2;
return rotateArray[midIndex];
}
midIndex=(index1+index2)/2;
//三者相等
if(rotateArray[index1]==rotateArray[midIndex] && rotateArray[midIndex]==rotateArray[index2])
return MinInOrder(rotateArray,index1,index2);
if(rotateArray[index1]<=rotateArray[midIndex])
index1=midIndex;
else if(rotateArray[index2]>=rotateArray[midIndex])
index2=midIndex;
}
return rotateArray[midIndex];
}
int MinInOrder(vector<int> rotateArray,int index1,int index2){
int result=rotateArray[index1];
int i=index1+1;
for(i;i<=index2;i++)
if(result>rotateArray[i])
result=rotateArray[i];
return result;
}
};
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
二:解题思路
非递减排列 :数字按照增加的顺序排列,其中有相等的数
如:
1,2,3,4,5,.:递增排列
9,8,7,6,5.:递减排列
1,2,3,3,4,5,8,8,.:非递减排列
9,8,7,7,6,5,5,2,1,.:非递增排列
题目中把排序数组的前面n个元素搬到最后面,可能n=0,要注意
第一个简单的想法:
旋转后的数组实际上可以划分为两个排序数组,而且前面的子数组的元素都大于等于后面子数组的元素,而最小元素刚好是这两个子数组的分界线
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0)
return 0;
if(rotateArray.size()==1)
return rotateArray[0];
int length=rotateArray.size();
int i;
for(i=0;i<=length-2;i++)
if(rotateArray[i]>rotateArray[i+1])
return rotateArray[i+1];
//如果数组转换后,是一个非递减排序,第一位是最小的
return rotateArray[0];
}
};
有时不要把题看的太简单,往往都是出题者的坑,套路,就算实现了,可能也不是最佳情况,剑指Offer给出的最佳结果是基于排序的思想
用两个指针分别指向两个子数组,第一个指针总是指向前面递增数组的元素,第二个指针总是指向后面递增的数组的元素,最终第一个指针指向前面子数组的最后一个元素,第二个指针指向后面数组的第一个元素,他们最终指向两个相邻的元素,而第二个指针刚好指向最小的元素。
特殊情况
当第一个指针,中间位置,与第二个指针所指元素相同的时候怎么办呢?采用顺序查找
三:代码实现
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0)
return 0;
if(rotateArray.size()==1)
return rotateArray[0];
int index1=0;
int index2=rotateArray.size()-1;
int midIndex=index1;
while(rotateArray[index1]>=rotateArray[index2]){
if(index2-index1==1){
midIndex=index2;
return rotateArray[midIndex];
}
midIndex=(index1+index2)/2;
//三者相等
if(rotateArray[index1]==rotateArray[midIndex] && rotateArray[midIndex]==rotateArray[index2])
return MinInOrder(rotateArray,index1,index2);
if(rotateArray[index1]<=rotateArray[midIndex])
index1=midIndex;
else if(rotateArray[index2]>=rotateArray[midIndex])
index2=midIndex;
}
return rotateArray[midIndex];
}
int MinInOrder(vector<int> rotateArray,int index1,int index2){
int result=rotateArray[index1];
int i=index1+1;
for(i;i<=index2;i++)
if(result>rotateArray[i])
result=rotateArray[i];
return result;
}
};
相关文章推荐
- 剑指offer——面试题8:旋转数组的最小数字
- 剑指offer 面试题8—旋转数组的最小数字
- 【剑指offer】面试题八:旋转数组的最小数字
- [剑指offer][面试题08]求旋转数组的最小数字
- 【剑指Offer面试题】 九度OJ1386:旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 剑指Offer---面试题8:旋转数组的最小数字
- 剑指Offer——面试题8:旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 旋转数组的最小数字(剑指offer面试题)
- 【剑指Offer学习】【面试题8 : 旋转数组的最小数字】
- 【剑指offer】面试题8:旋转数组的最小数字
- 【练习笔记】剑指offer-面试题8 :旋转数组的最小数字
- 剑指offer之面试题8:旋转数组的最小数字
- 剑指offer-面试题8-旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组中的最小数字
- 剑指offer面试题[8]-旋转数组的最小数字
- 剑指offer-面试题8-旋转数组的最小数字