链接: https://oj.leetcode.com/problems/find-peak-element/
题目要求复杂度在对数级以内
对数组下标进行二分搜索,
public class Solution { public int findPeakElement(int [] num) { int l=0,r=num.length-1; int mid=(l+r)/2; while(r-l>1) //按下标二分搜索 { mid=(l+r)/2; if(num[mid-1]<num[mid]&&num[mid]>num[mid+1]) return mid; if(num[mid-1]>num[mid+1]) { r=mid; continue; } if(num[mid-1]<num[mid+1]) { l=mid; continue; } if(num[mid-1]==num[mid+1]) { if(l==0) { if(num[l]>num[l+1]) return l; else { l++; continue; } } if(l!=0) { if(num[l-1]<num[l]&&num[l]>num[l+1]) return l; else { l++; continue; } } } } return num[l]>num[r]?l:r; } }