Find Peak Element
来源:互联网 发布:交友软件哪个好 编辑:程序博客网 时间:2024/05/17 08:33
O(n)时间复杂度的naive解法也可以AC,但题目要求O(logN)时间复杂度,说明测试用例并不严格。
O(N)解法:
class Solution {public: int findPeakElement(const vector<int> &num) { int n=num.size(); if(n==0) { return -1; } if(n==1) { return 0; } if(num[1]<num[0]) { return 0; } else { int index=1; while((index<n)&&(num[index-1]<num[index])) { index++; } return index-1; } }};
class Solution {public: int findPeakElement(const vector<int> &num) { int low = 0; int high = num.size()-1; while(low < high) { int mid1 = (low+high)/2; int mid2 = mid1+1; if(num[mid1] < num[mid2]) low = mid2; else high = mid1; } return low; }};
0 0
- [LeetCode] Find Peak Element
- Find Peak Element
- Leetcode:Find Peak Element
- Find Peak Element
- Find Peak Element
- Find Peak Element
- LeetCode Find Peak Element
- leetcode: Find Peak Element
- leetcode:Find Peak Element
- Find Peak Element
- Find Peak Element
- Find Peak Element
- LeetCode Find Peak Element
- Find Peak Element --leetcode
- Leetcode Find Peak Element
- [LeetCode] Find Peak Element
- Find Peak Element [Medium]
- Leetcode Find Peak Element
- java值传递面试题3
- Hibernate学习笔记之查询
- Android-JNI搭建以及调用
- iOS开发如何提高自己的水平
- 根域名301到带www的域名的方式及检测方法
- Find Peak Element
- 堆
- Python学习:读文件
- php利用openssl实现RSA非对称加密签名
- SQLite触发器使用
- 通过Java进行网页抓取并生成plist创建代码
- could not find class
- Web MVC简介
- atmega328p烧写Arduino bootloader