题目链接
思路一:
思路二:
int findPeakElement(int* nums, int numsLen ) {
// write code here
int left=0;
int right=numsLen-1;
int* cur=nums;
int mid=0;
//特殊情况判断两个值,单增和单减
if(cur[0]>cur[1])
{
return 0;
}
if(cur[numsLen-1]>cur[numsLen-2])
{
return numsLen-1;
}
//普遍情况
while(left<right)
{
//中间下标
mid=((left+right)/2);
//其他的正常判断
//上升
if((cur[mid-1]<cur[mid])&&(cur[mid]<cur[mid+1]))
{
left=mid;
}
//下降
else if((cur[mid-1]>cur[mid])&&(cur[mid]>cur[mid+1]))
{
right=mid;
}
//谷底
else if((cur[mid-1]>cur[mid])&&(cur[mid]<cur[mid+1]))
{
left=mid;
}
//峰值
else if((cur[mid-1]<cur[mid])&&(cur[mid]>cur[mid+1]))
{
return mid;
}
}
return -1;
}