hhh

#include <stdio.h>
#include <exception>
#include <iostream>


int findMinInRotatedArray(int data[], int staIndex, int endIndex)
{
if (staIndex > endIndex || data == NULL)
{
throw ParamException();
}

int minimum = data[staIndex];

if (data[staIndex] >= data[endIndex] && staIndex < endIndex - 1)  // 易错点
{
// Use Binary Search
int midIndex = (staIndex + endIndex) / 2;

if (data[midIndex] > data[staIndex])
minimum = findMinInRotatedArray(data, midIndex, endIndex);
else if (data[midIndex] < data[endIndex])
minimum = findMinInRotatedArray(data, staIndex, midIndex);
else
{
// Find the minimum sequentially
for (int i = staIndex+1; i <= endIndex; ++i)
if (minimum > data[i])
minimum = data[i];
}
}
else if (staIndex == (endIndex - 1))
{
minimum = data[staIndex] > data[endIndex]? data[endIndex]:data[staIndex];
}

return minimum;
}

全部评论

相关推荐

07-09 18:28
门头沟学院 Java
写着提前批,结果还要实习4个月以上???
程序员牛肉:这种不用看,直接投了,面试的时候问对应的HR就行。有可能他们是直接复制的暑期实习的模板。
点赞 评论 收藏
分享
流浪的神仙:无恶意,算法一般好像都得9硕才能干算法太卷啦
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务