1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-12 02:41:42 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/山脉数组中查找目标值 [find-in-mountain-array].html
2025-01-09 20:29:41 +08:00

55 lines
2.1 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>(这是一个 <strong>交互式问题&nbsp;</strong></p>
<p>你可以将一个数组&nbsp;<code>arr</code>&nbsp;称为&nbsp;<strong>山脉数组&nbsp;</strong>当且仅当:</p>
<ul>
<li><code>arr.length &gt;= 3</code></li>
<li>存在一些&nbsp;<code>0 &lt; i &lt; arr.length - 1</code>&nbsp;&nbsp;<code>i</code>&nbsp;使得:
<ul>
<li><code>arr[0] &lt; arr[1] &lt; ... &lt; arr[i - 1] &lt; arr[i]</code></li>
<li><code>arr[i] &gt; arr[i + 1] &gt; ... &gt; arr[arr.length - 1]</code></li>
</ul>
</li>
</ul>
<p>给定一个山脉数组&nbsp;<code>mountainArr</code>&nbsp;,返回&nbsp;<strong>最小</strong>&nbsp;<code>index</code>&nbsp;使得&nbsp;<code>mountainArr.get(index) == target</code>。如果不存在这样的&nbsp;<code>index</code>,返回&nbsp;<code>-1</code>&nbsp;</p>
<p><strong>你无法直接访问山脉数组</strong>。你只能使用&nbsp;<code>MountainArray</code>&nbsp;接口来访问数组:</p>
<ul>
<li><code>MountainArray.get(k)</code>&nbsp;返回数组中下标为&nbsp;<code>k</code>&nbsp;的元素(从 0 开始)。</li>
<li><code>MountainArray.length()</code>&nbsp;返回数组的长度。</li>
</ul>
<p>调用&nbsp;<code>MountainArray.get</code>&nbsp;超过&nbsp;<code>100</code>&nbsp;次的提交会被判定为错误答案。此外,任何试图绕过在线评测的解决方案都将导致取消资格。</p>
<ol>
</ol>
<p>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>3 &lt;= mountainArr.length() &lt;= 10<sup>4</sup></code></li>
<li><code>0 &lt;= target &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= mountainArr.get(index) &lt;=&nbsp;10<sup>9</sup></code></li>
</ul>