leetcode_1095. Find in Mountain Array_[Binary Search]
https://leetcode.com/problems/find-in-mountain-array/
题意:给定一个MountainArray(定义见题目),找到其中最早出现的target值的下标。
MountainArray.get() 函数调用不能超过100次。
解法:首先使用Binary Search找到mountain的peak,将array分为一个严格递增和一个严格递减的array,然后使用Binary Search。
class Solution
{
public:
int find_peak(MountainArray &mountainArr)
{
int l=, r=mountainArr.length()-;
while(l<=r)
{
int m=(l+r)/;
int m_val=mountainArr.get(m);
int m_r_val=mountainArr.get(m+);
if(m_val<m_r_val)
l=m+;
else
r=m-;
}
return l;
}
int findInMountainArray(int target, MountainArray &mountainArr)
{
int peak=find_peak(mountainArr);
int l=, r=peak;
if(mountainArr.get(l)<=target&&mountainArr.get(r)>=target)
while(l<=r)
{
int m=(l+r)/;
int val=mountainArr.get(m);
if(val==target)
return m;
else if(val<target)
l=m+;
else
r=m-;
}
l=peak;
r=mountainArr.length()-;
if(mountainArr.get(r)<=target&&mountainArr.get(l)>=target)
while(l<=r)
{
int m=(l+r)/;
int val=mountainArr.get(m);
if(val==target)
return m;
else if(val<target)
r=m-;
else
l=m+;
}
return -;
}
};
leetcode_1095. Find in Mountain Array_[Binary Search]的更多相关文章
- Binary Search 二分法方法总结
Binary Search 二分法方法总结 code教你做人:二分法核心思想是把一个大的问题拆成若干个小问题,最重要的是去掉一半或者选择一半. 二分法模板: public int BinarySear ...
- [LeetCode] questions conclusion_ Binary Search
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
随机推荐
- Flutter实战视频-移动电商-40.路由_Fluro的全局注入和使用方法
40.路由_Fluro的全局注入和使用方法 路由注册到顶层,使每个页面都可以使用,注册到顶层就需要在main.dart中 main.dart注册路由 注入 onGenerateRoute是Materi ...
- Spring Boot 2.x(十七):快速入门Elastic Search
What -- Elasticsearch是什么? Elasticsearch是一个基于Lucene的搜索服务器,Elasticsearch也是使用Java编写的,它的内部使用Lucene做索引与搜索 ...
- TextBox禁用与启用的方法
$('#text1').textbox('enable');// 启用 $('#text1').textbox('disable');// 禁用 注:以上方法也适用于datebox等等
- JS时间框架之舍弃Moment.js拥抱Day.js
什么是Day.js Day.js 是一个轻量的处理时间和日期的 JavaScript 库,和 Moment.js 的 API 设计保持完全一样. 如果您曾经用过 Moment.js, 那么您已经知道如 ...
- 洛谷 P2623 物品选取
https://www.luogu.org/problemnew/show/P2623 https://www.luogu.org/blog/test-1/solution-p2623 重点就是甲类物 ...
- Spring+Quartz配置定时任务
一.Quartz介绍 在企业应用中,我们经常会碰到时间任务调度的需求,比如每天凌晨生成前天报表,每小时生成一次汇总数据等等.Quartz是出了名的任务调度框架,它可以与J2SE和J2EE应用程序相结合 ...
- [未读]深入浅出node.js
还没看过,据说很多内容来自国外译文.
- QlikView入门
1.windows x64下载地址: http://d1cf4w4kkla6tb.cloudfront.net/qlikview/11.20/11718/_MSI/QlikViewDesktop_x6 ...
- WebForm随笔
一般处理程序中获取页面所传的值:int id = Convert.ToInt32(context.Request["id"]); 后台获取页面所传的值:int id = Conve ...
- div 绝对定位
div绝对居下 .Phone2title{ width:%; height:30px; line-height:30px; /*text-align:left;*/ /*background-colo ...