说要写成对数时间复杂度,算了想不出来,写个O(n)的水了

class Solution {
public:
int findPeakElement(const vector<int> &num) {
int len = num.size();
if (len < ) {
return -;
}
if (len == ) {
return ;
}
bool asc = true;
int idx = ;
int last = num[idx++]; while (idx < len) {
int cur = num[idx];
if (asc) {
if (cur < last) {
return idx - ;
}
} else {
if (cur > last) {
asc = true;
}
}
last = cur;
idx++;
}
if (asc) {
return idx - ;
}
return -;
}
};

第二轮:

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.

click to show spoilers.

Note:

Your solution should be in logarithmic complexity.

O(n)的

 // 9:43
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
if (len == ) {
return ;
}
if (nums[] > nums[]) {
return ;
} for (int i=; i<len-; i++) {
if (nums[i] > nums[i-] && nums[i] > nums[i+]) {
return i;
}
}
return len-;
}
};

从discuss(https://leetcode.com/discuss/23840/java-binary-search-solution)里找到一个logn的但是不是很明白:

 class Solution {
public:
int findPeakElement(vector<int>& nums) {
int len = nums.size();
int lo = , hi = len - ; while (lo < hi) {
int mid = (lo + hi) / ;
if (nums[mid] > nums[mid + ]) {
hi = mid;
} else {
lo = mid + ;
}
}
return lo;
}
};

由于规定了边界元素特征,在二分搜索的时候,都使得每个子空间尝试满足这个条件

LeetCode Find Peak Element [TBD]的更多相关文章

  1. [LeetCode] Find Peak Element 求数组的局部峰值

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  2. LeetCode Find Peak Element

    原题链接在这里:https://leetcode.com/problems/find-peak-element/ 题目: A peak element is an element that is gr ...

  3. LeetCode Find Peak Element 找临时最大值

    Status: AcceptedRuntime: 9 ms 题意:给一个数组,用Vector容器装的,要求找到一个临时最高点,可以假设有num[-1]和num[n]两个元素,都是无穷小,那么当只有一个 ...

  4. LeetCode: Find Peak Element 解题报告

    Find Peak Element A peak element is an element that is greater than its neighbors. Given an input ar ...

  5. [LeetCode] Find Peak Element 二分搜索

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  6. Lintcode: Find Peak Element

    There is an integer array which has the following features: * The numbers in adjacent positions are ...

  7. LeetCode OJ 162. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  8. LeetCode 162. Find Peak Element (找到峰值)

    A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...

  9. (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element

    A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...

随机推荐

  1. Python 中的 10 个常见安全漏洞,以及如何避免(下)

    简评:编写安全代码很困难,当你学习一个编程语言.模块或框架时,你会学习其使用方法. 在考虑安全性时,你需要考虑如何避免被滥用,Python 也不例外,即使在标准库中,也存在用于编写应用的不良实践.然而 ...

  2. UITextInputMode

    An instance of the UITextInputMode class represents the current text-input mode. You can use this ob ...

  3. #PHP 数组添加元素、统计数组相同元素个数、改变数组key值~_~

    一.数组添加元素 1.定义和用法: array_push() 函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度. 2.语法: array_push(array,value1, ...

  4. I2C裸机驱动程序设计

    ① I2C(Inter-Integrated Circuit)总线是由飞利浦公司开发的两线式串行总线,用于连接微控制器及其外围设备 ② I2C总线有两根双向信号线 (1)SDA:Serial Data ...

  5. 1-----Docker实例-安装Nginx

    Docker 安装 Nginx 方法一.docker pull nginx(推荐) 查找 Docker Hub 上的 nginx 镜像 runoob@runoob:~/nginx$ docker se ...

  6. $bzoj1021-SHOI2008\ Debt$ 循环的债务 $dp$

    题面描述 \(Alice\).\(Bob\)和\(Cynthia\)总是为他们之间混乱的债务而烦恼,终于有一天,他们决定坐下来一起解决这个问题.不过,鉴别钞票的真伪是一件很麻烦的事情,于是他们决定要在 ...

  7. python any和all

    摘自<流畅的Python> all 和 any 也是内置的归约函数. all(iterable) 如果 iterable 的每个元素都是真值,返回 True:all([]) 返回 True ...

  8. 源码安装caffe2时遇到的问题解决办法

    https://github.com/facebookresearch/DensePose/issues/119

  9. (转)Shell常用的特殊位置参数变量说明

    Shell常用的特殊位置参数变量说明 原文:http://m.blog.itpub.net/15498/viewspace-2151142/ $0    获取当前执行的shell脚本的文件名,如果执行 ...

  10. 004-C3P0连接池工具类模板

    package ${enclosing_package}; import java.sql.Connection; import java.sql.ResultSet; import java.sql ...