【leetcode】1095. Find in Mountain Array
题目如下:
(This problem is an interactive problem.)
You may recall that an array
A
is a mountain array if and only if:
A.length >= 3
- There exists some
i
with0 < i < A.length - 1
such that:
A[0] < A[1] < ... A[i-1] < A[i]
A[i] > A[i+1] > ... > A[A.length - 1]
Given a mountain array
mountainArr
, return the minimumindex
such thatmountainArr.get(index) == target
. If such anindex
doesn't exist, return-1
.You can't access the mountain array directly. You may only access the array using a
MountainArray
interface:
MountainArray.get(k)
returns the element of the array at indexk
(0-indexed).MountainArray.length()
returns the length of the array.Submissions making more than
100
calls toMountainArray.get
will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.Example 1:
Input: array = [1,2,3,4,5,3,1], target = 3
Output: 2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.Example 2:
Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist inthe array,
so we return -1.Constraints:
3 <= mountain_arr.length() <= 10000
0 <= target <= 10^9
0 <= mountain_arr.get(index) <= 10^9
解题思路:我的解法是二分查找。mountain array 数组的特点是有一个顶点,顶点左边的区间是单调递增,右边的区间是单调递减。所以首先是找出顶点的下标,对于任意一个点mid,如果值比(mid-1)和(mid+1)都大,表示这个是顶点;如果mid的值大于(mid+1),表示mid处于下降区间,令high = mid - 1;如果mid的值大于(mid-1),表示mid处于上升区间,令low = mid + 1;最终可以计算出顶点top。接下来再对左边的上升区间做二分查找求target,如果找到则返回对应小标;没有的话继续对右边的下降区间用二分查找。
代码如下:
# """
# This is MountainArray's API interface.
# You should not implement it, or speculate about its implementation
# """
#class MountainArray(object):
# def get(self, index):
# """
# :type index: int
# :rtype int
# """
#
# def length(self):
# """
# :rtype int
# """ class Solution(object):
def findInMountainArray(self, target, mountain_arr):
"""
:type target: integer
:type mountain_arr: MountainArray
:rtype: integer
"""
length = mountain_arr.length()
low = 0
high = length - 1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
mid_l_val,mid_h_val = -float('inf'),-float('inf')
if mid - 1 >= 0:
mid_l_val = mountain_arr.get(mid-1)
if mid + 1 <= high:
mid_h_val = mountain_arr.get(mid+1)
if mid_val > mid_l_val and mid_val > mid_h_val:
break
elif mid_val > mid_h_val:
high = mid - 1
elif mid_val > mid_l_val:
low = mid + 1
#left
low,high = 0,mid
res = -1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
if target == mid_val:
res = mid
break
elif target > mid_val:
low = mid + 1
else:
high = mid - 1 if res != -1:return res
low, high = mid,length-1
while low <= high:
mid = (low + high)/2
mid_val = mountain_arr.get(mid)
if target == mid_val:
res = mid
break
elif target < mid_val:
low = mid + 1
else:
high = mid - 1
return res
【leetcode】1095. Find in Mountain Array的更多相关文章
- 【LeetCode】697. Degree of an Array 解题报告
[LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...
- 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值
[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【LeetCode】Two Sum II - Input array is sorted
[Description] Given an array of integers that is already sorted in ascending order, find two numbers ...
- 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...
- 【leetcode】Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
- 【题解】【数组】【查找】【Leetcode】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】697. Degree of an Array 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- Octavia 项目加速 OpenStack LBaaS 落地大规模应用场景
目录 文章目录 目录 OpenStack LBaaS Octavia 软件架构 网络架构 操作对象基本概念 功能实现基本概念 Ocatvia Daemon 列表 部署 Ocatvia 手动方式集成 O ...
- Mysql登录报1045错误
MySQL在使用root密码登陆报 1045 ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password ...
- 你知道e.g.和i.e.的区别吗?
见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.
- 深入理解java:2.3.1. 并发编程concurrent包 之Atomic原子操作(循环CAS)
java中,可能有一些场景,操作非常简单,但是容易存在并发问题,比如i++, 此时,如果依赖锁机制,可能带来性能损耗等问题, 于是,如何更加简单的实现原子性操作,就成为java中需要面对的一个问题. ...
- 深入理解java:2. 多线程机制
引言 很多人都对其中的一些概念不够明确,如同步.并发等等,让我们先理清一些概念,以免产生误会. 多线程:指的是这个程序(一个进程)运行时,产生了不止一个线程. 并行与并发: 并行:多个cpu实例或者多 ...
- 第四周预习作业and第五周作业
第四周预习作业 统计一行文本的单词个数 本题目要求编写程序统计一行字符中单词的个数.所谓"单词"是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个. 输入格式: 输入 ...
- Java简易实现记事本的打开与保存
记事本的打开与保存 一些总结 * Swing中有时方法不显示,需要把方setVisible(true)放到最后执行 * AWT中的TextArea默认是中间布局 * fileDialog对话框Load ...
- spring cloud gateway自定义过滤器
在API网关spring cloud gateway和负载均衡框架ribbon实战文章中,主要实现网关与负载均衡等基本功能,详见代码.本节内容将继续围绕此代码展开,主要讲解spring cloud g ...
- 《剑指offer》面试题24 二叉搜索树的后序遍历序列 Java版
(判断一个元素均不相同的序列是否为一个BST的LRD) 书中方法:首先对于二叉搜索树,左子树中的所有元素小于根节点小于右子树中的所有元素,然后后序遍历序列最后一个元素是根节点,这是我们已知的条件.这道 ...
- 小z的洞穴之旅 QDUOJ 并查集+连通块
小z的洞穴之旅 QDUOJ 并查集+连通块 原题链接 题意 小 z 同学在某个闲暇的周末决定去野外探险一波,结果在丛林深处中误打误撞进入了一个神秘的洞穴,虽然洞穴中光线昏暗,但小 z 凭借其敏锐的眼力 ...