20210625 852 山脉数组的峰顶索引

题目链接:852. 山脉数组的峰顶索引 - 力扣(LeetCode) (leetcode-cn.com)

三分搜索

cpp class Solution { public: int peakIndexInMountainArray(vector<int>& a) { int l = 0; int r = a.size()-1; while(l<r) { int m = l + (r-l) /2; int ml = l + (m-l)/2 ; int mr = r - (r-m) / 2; // cout<<m<<" "<<l<<" "<<r<<" "<<ml<<" "<<mr<<endl; if(a[ml]>=a[mr]) { r = mr-1; } else if(a[ml]<a[mr]) { l = ml+1; } } return l; } };


最后更新: 2022-11-15

评论