mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-12 02:41:42 +08:00
55 lines
2.1 KiB
HTML
55 lines
2.1 KiB
HTML
<p>(这是一个 <strong>交互式问题 </strong>)</p>
|
||
|
||
<p>你可以将一个数组 <code>arr</code> 称为 <strong>山脉数组 </strong>当且仅当:</p>
|
||
|
||
<ul>
|
||
<li><code>arr.length >= 3</code></li>
|
||
<li>存在一些 <code>0 < i < arr.length - 1</code> 的 <code>i</code> 使得:
|
||
<ul>
|
||
<li><code>arr[0] < arr[1] < ... < arr[i - 1] < arr[i]</code></li>
|
||
<li><code>arr[i] > arr[i + 1] > ... > arr[arr.length - 1]</code></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
<p>给定一个山脉数组 <code>mountainArr</code> ,返回 <strong>最小</strong> 的 <code>index</code> 使得 <code>mountainArr.get(index) == target</code>。如果不存在这样的 <code>index</code>,返回 <code>-1</code> 。</p>
|
||
|
||
<p><strong>你无法直接访问山脉数组</strong>。你只能使用 <code>MountainArray</code> 接口来访问数组:</p>
|
||
|
||
<ul>
|
||
<li><code>MountainArray.get(k)</code> 返回数组中下标为 <code>k</code> 的元素(从 0 开始)。</li>
|
||
<li><code>MountainArray.length()</code> 返回数组的长度。</li>
|
||
</ul>
|
||
|
||
<p>调用 <code>MountainArray.get</code> 超过 <code>100</code> 次的提交会被判定为错误答案。此外,任何试图绕过在线评测的解决方案都将导致取消资格。</p>
|
||
|
||
<ol>
|
||
</ol>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>mountainArr = [1,2,3,4,5,3,1], target = 3
|
||
<strong>输出:</strong>2
|
||
<strong>解释:</strong>3 在数组中出现了两次,下标分别为 2 和 5,我们返回最小的下标 2。</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<pre>
|
||
<strong>输入:</strong>mountainArr = [0,1,2,4,2,1], target = 3
|
||
<strong>输出:</strong>-1
|
||
<strong>解释:</strong>3 在数组中没有出现,返回 -1。
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>3 <= mountainArr.length() <= 10<sup>4</sup></code></li>
|
||
<li><code>0 <= target <= 10<sup>9</sup></code></li>
|
||
<li><code>0 <= mountainArr.get(index) <= 10<sup>9</sup></code></li>
|
||
</ul>
|