[LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features:
- The numbers in adjacent positions are different.
- A[0] < A[1] && A[A.length - 2] > A[A.length - 1].
We define a position P is a peek if:
A[P] > A[P-1] && A[P] > A[P+1]
Find a peak element in this array. Return the index of the peak.
Notice
The array may contains multiple peeks, find any of them.
Given [1, 2, 1, 3, 4, 5, 7, 6]
Return index 1
(which is number 2) or 6
(which is number 7)
Time complexity O(logN)
LeetCode上的原题,请参见我之前的博客Find Peak Element。
解法一:
class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
int left = , right = A.size() - ;
while (left < right) {
int mid = left + (right - left) / ;
if (A[mid] < A[mid + ]) left = mid + ;
else right = mid;
}
return right;
}
};
解法二:
class Solution {
public:
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
int findPeak(vector<int> A) {
for (int i = ; i < A.size(); ++i) {
if (A[i] < A[i - ]) return i - ;
}
return A.size() - ;
}
};
[LintCode] Find Peak Element 求数组的峰值的更多相关文章
- [LeetCode] Find Peak Element 求数组的局部峰值
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- LintCode "Find Peak Element II"
Idea is the same: climbing up the hill along one edge (Greedy)! Visualize it in your mind! class Sol ...
- lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道, 这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值, 而 ...
- [Swift]LeetCode162. 寻找峰值 | Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- Leetcode之二分法专题-162. 寻找峰值(Find Peak Element)
Leetcode之二分法专题-162. 寻找峰值(Find Peak Element) 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1] ...
- [LeetCode] 162. Find Peak Element 查找峰值元素
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
- LeetCode 162. 寻找峰值(Find Peak Element) 29
162. 寻找峰值 162. Find Peak Element 题目描述 峰值元素是指其值大于左右相邻值的元素. 给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元 ...
- LeetCode 162. Find Peak Element (找到峰值)
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- Python~字典
if not isinstance(x, (int, float)): raise TypeError('bad operand type') range() raw_input(‘birth’) ...
- check time period
/** * @author etao * @description check last time selected * @param timePeriod * @pa ...
- Eclipse 一些小知识
快速查找未完成事项 eg: // TODO 通过模板格式化代码 Window --> Preferences --> Java --> Editor --> Template ...
- 【转】Caffe初试(九)solver及其设置
solver算是caffe的核心的核心,它协调着整个模型的运作.caffe程序运行必带的一个参数就是solver配置文件.运行代码一般为 #caffe train --solver=*_solver. ...
- Maven之安装与简单入门一
Maven 是一个项目管理和构建自动化工具,我们最关心的是它的项目构建功能.Maven 使用惯例优于配置的原则. 1,下载安装包,并根据文档说明安装: http://maven.apache.org/ ...
- c语言学习笔记三
第三章,函数 字符串函数 //strcmp int my_strcmp(char *str1,char *str2) { while(*str1 = = *str2) /*不可用while(* ...
- ubuntu下设置数据库字符集
就linux环境下出现的数据库乱码的问题,以ubuntu为例进行的总结 ubuntu环境设置的字符集utf8,windows默认字符集是GBK,Ubuntu的默认字符集为utf-8,这使 得在用tel ...
- MapReduce工作原理图文详解
目录:1.MapReduce作业运行流程2.Map.Reduce任务中Shuffle和排序的过程 1.MapReduce作业运行流程 流程示意图: 流程分析: 1.在客户端启动一个作业. 2.向Job ...
- json 排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Git安装
前面说了很多废话,说得再好,还不如实践一次.要想使用Git,首先得安装.这次实验主要是实践怎样安装Git. 安装与检查是否安装成功 1. 在Linux(Ubuntu)上安装 上篇的Git简介已经介绍过 ...