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 ...
随机推荐
- 对比<input type="text" id="">和<asp:TextBox runat="server" ID="">
首先这两个都是对应文本输入框形式: <input type="text"class="form-control"id="txt_add_pro_ ...
- LeetCode: 620 Not Boring Movies(easy)
题目: X city opened a new cinema, many people would like to go to this cinema. The cinema also gives o ...
- C/C++内存检测工具Valgrind
内存检测Valgrind简介 Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,作者是获得过Google-O'Reilly开源大奖的Julian Seward, 它包含一个内核 ...
- 浅谈SpringBoot核心注解原理
SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置 ...
- Swift3.0 字典简单使用
Dictionary //定义一个只能存放string 类型的字典 var dict:Dictionary<String,String> = ["一班":"2 ...
- [已读]JavaScript DOM编程艺术
看到过很多人将它作为推荐入门书籍,当时我刚看完ppk和javascript精粹,于是看到这本就觉得很一般了.怎么说,它适合基础.
- 牛客网Java刷题知识点之Iterator和ListIterator的区别
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=21 ...
- 怎么样去优化我们的SQL语句
1.改写in 在SQL语言中,一个查询块可以作为另一个查询块中谓词的一个操作数.因此,SQL查询可以层层嵌套.例如在一个大型分布式数据库系统中,有订单表Order.订单信息表OrderDetail,如 ...
- P1281 书的复制
题目描述 现在要把m本有顺序的书分给k给人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如不能把第一.第三.第四本书给同一个人抄写. ...
- laravel 错误总结
1.ReflectionException (-1) Class PhotosController does not exist 原因: 资源路由的问题 ,命名空间要区分大小写,admin首字母要大写 ...