【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/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...
随机推荐
- 红帽虚拟化RHEV-安装RHEV-M
目录 目录 前言 软件环境 时间同步 更新系统 安装并配置RHEV-M 添加域并为用户授权远程登陆 安装rhevm报告 安装Spice协议 最后 前言 在红帽虚拟化RHEV-架构简介篇中介绍了RHEV ...
- wcf restful 访问报错 *.svc HTTP error 404.17 - Not Found
安装完成 iisreset,即使不重启也已经可以使用了
- 如何在linux上部署vue项目
安装nginx的前奏 安装依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 创建一个文件夹 cd /usr/ ...
- centos7:storm集群环境搭建
1.安装storm 下载storm安装包 在线下载 wget http://apache.fayea.com/storm/apache-storm-1.1.1/apache-storm-1.1.1.t ...
- python接口自动化:requests+ddt+htmltestrunner数据驱动框架
该框架分为四个包:xc_datas.xc_driven.xc_report.xc_tools. xc_datas:存放数据,xc_driven:存放执行程序,xc_report:存放生成的报告,xc_ ...
- 【ABAP系列】SAP 系统的消息类型分析 MESSAGE TYPE
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 系统的消息类型分析 ME ...
- 【BZOJ2622】[2012国家集训队测试]深入虎穴
虎是中国传统文化中一个独特的意象.我们既会把老虎的形象用到喜庆的节日装饰画上,也可能把它视作一种邪恶的可怕的动物,例如“武松打虎”或者“三人成虎”.“不入虎穴焉得虎子”是一个对虎的威猛的形象的极好体现 ...
- python+selenium下载文件——firefox
修改Firefox的相关配置. 1.profile.set_preference('browser.download.folderList',2) 设置成0代表桌面,1代表下载到浏览器默认下载路径:2 ...
- Python-自定义函数-参数
一.自定义函数参数 1.种类 (1)位置参数 "x"就是位置参数 #!/usr/bin/env python # -*- coding: utf-8 -*- #author: di ...
- 【烦人的字符集】linux字符集问题,中文乱码
[1]快速修改命令 [2]locale 查看现在服务器的字符 [root@Master ~]# localeLANG=en_US.UTF-8LC_CTYPE="zh_CN.UTF-8&quo ...