lintcode 75 Find Peak Element
Hi 大家,这道题是lintcode上的find peak element的题,不是leecode的那道,
这两道题是有区别的,这道题的题目中说明了:只有左右两侧的数都小于某个元素,这种才是峰值,
而leetcode的题,是只要找到个最大值就行,可以是[1,2]这种。
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.
Example
Given [1, 2, 1, 3, 4, 5, 7, 6]
Return index 1
(which is number 2) or 6
(which is number 7)
分析:
你给出一个整数数组(size为n),其具有以下特点:
- 相邻位置的数字是不同的
- A[0] < A[1] 并且 A[n - 2] > A[n - 1]
假定P是峰值的位置则满足A[P] > A[P-1]
且A[P] > A[P+1]
,返回数组中任意一个峰值的位置。
给出数组[1, 2, 1, 3, 4, 5, 7, 6]
返回1
, 即数值 2 所在位置, 或者6
, 即数值 7 所在位置.
要找峰值,要分析每个点有哪几种情况,每个只有四种情况,
(1)在某个峰值
(2)在某个上升区间
(3)在某个下降区间
(4)在一个谷底,比左右两边的元素都小。
首先我们可以确定的是,第一个元素和最后一个元素不可能构成一个峰值,因为峰值要求某个值要同时大于左右两侧的数。
限定了start和end的范围,这道题我们用二分法解决。
第三个条件选择里 else{ start = mid} 也可以是 end = mid。
class Solution {
/**
* @param A: An integers array.
* @return: return any of peek positions.
*/
public int findPeak(int[] A) {
int start = 1, end = A.length-2; // 1.答案在之间,2.不会出界
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(A[mid] < A[mid - 1]) {
end = mid;
} else if(A[mid] < A[mid + 1]) {
start = mid;
} else {
start = mid;
}
}
if(A[start] < A[end]) {
return end;
} else {
return start;
}
}
}
lintcode 75 Find Peak Element的更多相关文章
- (二分查找 拓展) 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 ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- lintcode : find peak element 寻找峰值
题目 寻找峰值 你给出一个整数数组(size为n),其具有以下特点: 相邻位置的数字是不同的 A[0] < A[1] 并且 A[n - 2] > A[n - 1] 假定P是峰值的位置则满足 ...
- [LintCode] Find Peak Element 求数组的峰值
There is an integer array which has the following features: The numbers in adjacent positions are di ...
- Lintcode: Find Peak Element
There is an integer array which has the following features: * The numbers in adjacent positions are ...
- 【Lintcode】075.Find Peak Element
题目: There is an integer array which has the following features: The numbers in adjacent positions ar ...
- [LeetCode] 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
Problem: A peak element is an element that is greater than its neighbors. Given an input array where ...
- Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array where num[i] ≠ ...
随机推荐
- asp.net mvc @Html.Raw 作用
转自:http://zhidao.baidu.com/link?url=unayXHAylQiUF0E3Rc9ej4gz_XBC7sbwInupVFuDnp_Cuqdz5NzMyUK5u-HiSfif ...
- 9.12 其他样式;JS
Display 显示block和隐藏none,不占位置Visbility 显示visible和隐藏hidden,占位置Overflow 超出范围 hidden隐藏透明圆角 Js脚本语言(JavaScr ...
- phpcms从表v9_news_data中字段content中用正则取出图片的地址输出
preg_match ("<img.*src=[\"](.*?)[\"].*?>",$test,$match); echo "$match ...
- bootstrap弹框
http://v3.bootcss.com/javascript/#modals 参考bootstrap官网 模态框做php后端 前端一直不行,但是很多时候 用到ajax都要用到弹框,一直在代码里面找 ...
- PyQ1_介绍
PyQt是一个创建GUI应用程序的工具包.它是Python编程语言和Qt库的成功融合.Qt库是目前最强大的库之一.PyQt是由Phil Thompson 开发. PyQt实现了一个Python模块 ...
- FCC上的初级算法题
核心提示:FCC的算法题一共16道.跟之前简单到令人发指的基础题目相比,难度是上了一个台阶.主要涉及初步的字符串,数组等运算.仍然属于基础的基础,官方网站给出的建议完成时间为50小时,超出了之前所有非 ...
- Winsock 入门 计算校验和 示例
#include <stdio.h> #include <string.h> #define DATA_MAX_LEN 14 /* 最大数据长度 */ struct data_ ...
- CMAKE使用
http://www.cppblog.com/tx7do/archive/2010/08/19/124000.html http://blog.csdn.net/dbzhang800/article/ ...
- Thread 与 Runnable
在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...
- Mysql 常用函数
统计函数: count() 统计记录条数,如 select count(*) from stu; sum() 统计记录字段的和,如select sum(salary) from emp; ...