题目如下:

(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 with 0 < 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 minimum index such that mountainArr.get(index) == target.  If such an index 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 index k (0-indexed).
  • MountainArray.length() returns the length of the array.

Submissions making more than 100 calls to MountainArray.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 in the array, so we return -1.

Constraints:

  1. 3 <= mountain_arr.length() <= 10000
  2. 0 <= target <= 10^9
  3. 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的更多相关文章

  1. 【LeetCode】697. Degree of an Array 解题报告

    [LeetCode]697. Degree of an Array 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/degree- ...

  2. 【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 ...

  3. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  4. 【LeetCode】1095. 山脉数组中查找目标值 Find in Mountain Array

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetco ...

  5. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  6. 【题解】【数组】【查找】【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 ...

  7. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  8. 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...

  9. 【LeetCode】697. Degree of an Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求出最短相同子数组度的长度 使用堆求最大次数和最小长 ...

随机推荐

  1. oracle-参数文件的备份与还原

    oracle-参数文件的备份与还原 参数文件是实例启动到nomount状态的必要条件,规定了实例的行为特征,位置跟操作系统相关,一般unix类的系统在$ORACLE_HOME/dbs目录下 (wind ...

  2. appium常见问题01_android筛选下拉框无法定位问题

    近期用appium做android自动化的过程中,遇到一种筛选下拉框,神奇的是,定位工具定位怎样都定位不到. 首先尝试用uiaotomator工具定位,无法定位到下拉框元素,只能定位到底层元素: 询问 ...

  3. python每日一练:0007题

    第 0007 题: 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. # -*- coding:utf-8 -*- import os def count ...

  4. 【监控实践】【4.1】利用trace实现阻塞跟踪和慢查询跟踪

    原文:https://blog.csdn.net/kk185800961/article/details/49252037 分享个SQLServer profiler 的一个技巧吧.很早用过,忘记总结 ...

  5. [BZOJ 3771] Triple(FFT+容斥原理+生成函数)

    [BZOJ 3771] Triple(FFT+生成函数) 题面 给出 n个物品,价值为别为\(w_i\)且各不相同,现在可以取1个.2个或3个,问每种价值和有几种情况? 分析 这种计数问题容易想到生成 ...

  6. redis 教程(一)-基础知识

    redis 简介 redis 是高性能的 key-value 数据库,读的速度是110000次/s,写的速度是81000次/s ,它以内存作为主存储 具有以下优点: 1. 支持数据的持久化,将内存中的 ...

  7. Scrapy 教程(五)-分页策略

    scrapy 爬取分页网站的策略 1. 检测当前页是否存在“下一页” 2. 如果存在,把“下一页”的链接交给本方法或者其他方法 3. 如果不存在,结束 图示 示例代码 def parse(self, ...

  8. springboot 配置

    springboot 配置文件中属性变量引用方式@@解析 这种属性应用方式是field_name=@field_value@. 两个@符号是springboot为替代${}属性占位符产生,原因是${} ...

  9. HBase Shell 的常用操作总结

      1,创建表:create 't1','f1','f2','f3'                   #-------t1是表名,f1,f2,f3是列族名   2,查看所有的表:list   3, ...

  10. xilinx基础入门

    2019.09.03 一.基础部分及语法 一.FPGA程序的固化 [USF-XSim-62] 'simulate' step failed with errors. Please check the ...