LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)
题目标签:Binary Search
题目给了我们一组 int array,让我们找到数组的 peak。
利用 binary search, 如果数字比它后面那个数字小,说明还在上坡,缩小范围到右半边;
如果一个数字比它后面的大,说明是下坡,或者是peak,缩小范围到左半边,这里要包含mid,因为mid 可能是peak。具体看code。
Java Solution:
Runtime: 0 ms, faster than 100 %
Memory Usage: 39 MB, less than 85 %
完成日期:08/01/2019
关键点:比较相邻的数字来判断
class Solution {
public int peakIndexInMountainArray(int[] A) {
int left = 0;
int right = A.length-1;
while(left < right) {
int mid = left + (right - left) / 2;
if(A[mid] < A[mid + 1]) //up hill
left = mid + 1;
else // down hill or peak
right = mid;
}
return left;
}
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 852. Peak Index in a Mountain Array (山脉数组的峰顶索引)的更多相关文章
- LeetCode 852. Peak Index in a Mountain Array C++ 解题报告
852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...
- LeetCode 852 Peak Index in a Mountain Array 解题报告
题目要求 Let's call an array A a mountain if the following properties hold: A.length >= 3 There exist ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- leetcode 852. Peak Index in a Mountain Array
Input: [0,1,0] Output: 1 Input: [0,2,1,0] Output: 1解: 比较数组中的i和i-1的大小,如果前一位大于后一位数字,前一位则是结果 let ans = ...
- 【Leetcode_easy】852. Peak Index in a Mountain Array
problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...
- 【LeetCode】852. Peak Index in a Mountain Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 查找最大值位置 寻找第一个下降的位置 日期 ...
- [LeetCode] Peak Index in a Mountain Array 山形数组的顶峰坐标
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...
- 852. Peak Index in a Mountain Array
class Solution { public: int peakIndexInMountainArray(vector<int>& A) { return max_element ...
- Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array)
Leetcode之二分法专题-852. 山脉数组的峰顶索引(Peak Index in a Mountain Array) 我们把符合下列属性的数组 A 称作山脉: A.length >= 3 ...
随机推荐
- Shiro学习(18)并发人数限制
在某些项目中可能会遇到如每个账户同时只能有一个人登录或几个人同时登录,如果同时有多人登录:要么不让后者登录:要么踢出前者登录(强制退出).比如spring security就直接提供了相应的功能:Sh ...
- 解决(Missing artifact com.oracle:ojdbc14:jar:11.2.0.4.0)
maven项目检索时报Missing artifact com.oracle:ojdbc14:jar:11.2.0.4.0 经过查阅资料知道原因为: Oracle 的 ojdbc.jar 是收费的,M ...
- CSS:CSS 尺寸 (Dimension)
ylbtech-CSS:CSS 尺寸 (Dimension) 1.返回顶部 1. CSS 尺寸 (Dimension) CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允 ...
- 24、echarts做报表
1.今天就来讲一讲echarts的使用 使用步骤 (1)首先需要下载包echarts.js,然后引入该包, <!DOCTYPE html> <html> <header& ...
- 22、继续javascript,左边选中的跳到右边
1. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title& ...
- webpack中代理配置(proxyTable)
注:用axios请求 1,下载axios npm i axios --save 2,在config文件下的index.js中配置代理地址 参考:https://vuejs-templates.gith ...
- phoenix 利用CsvBulkLoadTool 批量带入数据并自动创建索引
需要先创建表: CREATE TABLE IF NOT EXISTS population ( state CHAR() NOT NULL, city VARCHAR NOT NULL, popula ...
- sql ibatis
<!-- 写入单位下当前参保人员 --> <insert id="insertTempCaz043" parameterClass="map" ...
- css3 鼠标悬停图片动画
<div class="grid"> <figure class="effect-milo"> <img src="im ...
- runtime机制
runtime(简称运行时),是一套 纯C(C和汇编写的) 的API.而 OC 就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 消息机制原理:对象根据方法编号SEL去映射表查找对 ...