# 【剑指offer】面试题8：旋转数组的最小数字

140人阅读 评论(0)

int FindMin(int* a, int size) {
int high = size - 1;
int low = 0;
int mid = (high + low) / 2;
while(low < high - 1) {
if (a[mid] > a[low]) {
low = mid;
} else {
high = mid;
}
mid = (low + high) / 2;
}

return a[high];
}

int FindMin(int* a, int size) {
int high = size - 1;
int low = 0;
int mid = (high + low) / 2;
while(low < high - 1) {
if (a[mid] > a[low]) {
low = mid;
} else {
high = mid;
}
mid = (low + high) / 2;
}
if (high == size - 1 && a[high] > a[low])
return a[0];

return a[high];
}

int FindMin(int* a, int size) {
if (a == NULL || size <= 0) {
cout << "Wrong input" << endl;
return -1;
}
int high = size - 1;
int low = 0;
int mid = (high + low) / 2;
while(low < high - 1) {
if (a[low] == a[high] && a[mid] == a[low]) { //先判断特殊情况
int minIndex = low;
for (int i = low; i < high; i ++) {
if (a[i] < a[minIndex]) minIndex = i;
}
return a[minIndex];
}

if (a[mid] > a[low]) {
low = mid;
} else {
high = mid;
}
mid = (low + high) / 2;
}
if (high == size - 1 && a[high] > a[low])
return a[0];

return a[high];
}
1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：49534次
• 积分：1484
• 等级：
• 排名：千里之外
• 原创：89篇
• 转载：23篇
• 译文：0篇
• 评论：11条
联系方式

微信：ilovevb23

邮箱：geek00021@gmail.com

评论排行
最新评论