【LeetCode】162.Find Peak Element 寻找峰值(二分法)
来源:互联网 发布:centos 7 minimal桌面 编辑:程序博客网 时间:2024/06/06 10:48
Find Peak Elemen
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞.
For example, in array [1, 2, 3, 1], 3 is a peak element and your function should return the index number 2.
寻找峰值
你给出一个整数数组(size为n),其具有以下特点:
1. 相邻位置的数字是不同的
2. A[0] < A[1] 并且 A[n - 2] > A[n - 1]
3. 假定P是峰值的位置则满足A[P] > A[P-1]且A[P] > A[P+1],返回数组中任意一个峰值的位置下标。
注意事项
It’s guaranteed the array has at least one peak.
The array may contain multiple peeks, find any of them.
The array has at least 3 numbers in it.
样例
给出数组[1, 2, 1, 3, 4, 5, 7, 6]返回1, 即数值 2 所在位置, 或者6, 即数值 7 所在位置.
挑战
Time complexity O(logN)
标签
二分法 数组 谷歌
(1)C++
法①#include <iostream>#include <cstdlib>#include <vector>#include <iterator>using namespace std;int LocalMaximum(const int* a, int size){ int left = 0, right = size - 1, mid; while (left < right) { mid = (left + right) / 2; cout<< "mid: " << mid<<endl; if(a[mid]>a[mid+1]){ right = mid; }else{ left = mid+1;//勿忘+1!!! } } return left;}int main(){ int a[] = {0,1,2,5,3,1}; int ret = LocalMaximum(a, sizeof(a)/sizeof(int)); cout<< "局部最大值下标:" << ret <<endl; return 0;}法②#include <iostream>#include <cstdlib>#include <vector>#include <iterator>using namespace std;int LocalMaximum(const int* a, int size){ if(!a || size == 0) return -1; int left = 0, right = size - 1, mid; while (left + 1 < right) { mid = (left + right) / 2; cout<< "mid: " << mid<<endl; if(a[mid]>a[mid+1]){ right = mid; }else{ left = mid; } } return a[left] > a[right] ? left : right;}int main(){ int a[] = {0,1,2,5,3,1}; int ret = LocalMaximum(a, sizeof(a)/sizeof(int)); cout<< "局部最大值下标:" << ret <<endl; return 0;}
(2)Java
class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { // write your code here int start = 1, end = A.length-2; // 1.答案在之间,2.不会出界 while(start + 1 < end) { int mid = (start + end) / 2; if(A[mid] < A[mid - 1]) { end = mid; } else if(A[mid] < A[mid + 1]) { start = mid; } else { end = mid; } } if(A[start] < A[end]) { return end; } else { return start; } }}
- 【LeetCode】162.Find Peak Element 寻找峰值(二分法)
- LeetCode 162. Find Peak Element(查找峰值)
- 162.Find Peak Element (寻找数组中的峰值点)
- 75.Find Peak Element-寻找峰值(中等题)
- [Lintcode]Find Peak Element 寻找峰值
- Leetcode 162 Find Peak Element 查找峰值元素(极大值)
- LeetCode-162. Find Peak Element (JAVA)寻找peak元素
- [C++]LeetCode: 118 Find Peak Element (二分查找 寻找数组局部峰值)
- LintCode Find Peak Element(查找峰值)
- Find Peak Element:寻找数列中的局部峰值
- [LeetCode]162.Find Peak Element
- LeetCode 162. Find Peak Element
- [leetcode] 162.Find Peak Element
- 【LeetCode】162. Find Peak Element
- Leetcode 162. Find Peak Element
- [LeetCode]162. Find Peak Element
- 162. Find Peak Element LeetCode
- [LeetCode]162. Find Peak Element
- 2017/10/03 学习笔记
- C语言只能在开头定义变量?
- [简单题]Counting Duplicates( Python 实现)
- 99%的人不满意现在的工作,可是然后呢?
- 请停止无效努力,让你的付出都能有所收获
- 【LeetCode】162.Find Peak Element 寻找峰值(二分法)
- GDB 调试使用总结
- JavaWeb项目实现上传文件动态显示进度百分比
- 为什么jsonp只支持get请求?JSONP是同步还是异步
- qml鼠标事件传递
- 数据的共享与保护
- [La Salle-Pui Ching Programming Challenge 培正喇沙編程挑戰賽 2017]Ambiguous Dates
- Spring/Boot/Cloud系列知识(5)——Spring EL(1)
- java 调度器 Timer