- 先减后增数组找最大
- public int turningPoint(int[] A) {
- int m = A.length;
- int begin = 0;
- int end = m - 1;
- int tp = begin + (end - begin)/2;
- // the condition "tp > 0 && tp < m -1" makes sure that tp is not at the beginning or the end
- while (tp > 0 && tp < m -1) {
- if (A[tp] > A[tp + 1] && A[tp] > A[tp -1]) {
- return tp;
- } else if (A[tp] < A[tp+1]) {
- begin = tp + 1;
- tp = begin + (end - begin)/2;
- } else {
- end = tp - 1;
- tp = begin + (end - begin)/2;
- }
- }
- return -1;
【算法】--- 先减后增数组找最大,先增后减数组找最小,先增后增找最小
最新推荐文章于 2023-09-13 18:50:15 发布
本文介绍了一种在先减后增数组中查找最大值的高效算法。通过使用二分搜索策略,算法能在O(log n)的时间复杂度内找到数组的转折点,即最大值所在位置。该方法避免了线性搜索的高时间成本,适用于处理大规模数据集。
866

被折叠的 条评论
为什么被折叠?



