solution.md 1.7 KB
Newer Older
ToTensor's avatar
ToTensor 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
# 寻找峰值

<p>峰值元素是指其值严格大于左右相邻值的元素。</p>

<p>给你一个整数数组&nbsp;<code>nums</code>,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 <strong>任何一个峰值</strong> 所在位置即可。</p>

<p>你可以假设&nbsp;<code>nums[-1] = nums[n] = -∞</code></p>

<p>你必须实现时间复杂度为 <code>O(log n)</code><em> </em>的算法来解决此问题。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>nums = <code>[1,2,3,1]</code>
<strong>输出:</strong>2
<strong>解释:</strong>3 是峰值元素,你的函数应该返回其索引 2。</pre>

<p><strong>示例&nbsp;2:</strong></p>

<pre>
<strong>输入:</strong>nums = <code>[</code>1,2,1,3,5,6,4]
<strong>输出:</strong>1 或 5 
<strong>解释:</strong>你的函数可以返回索引 1,其峰值元素为 2;
&nbsp;    或者返回索引 5, 其峰值元素为 6。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li>对于所有有效的 <code>i</code> 都有 <code>nums[i] != nums[i + 1]</code></li>
</ul>


## template

```python
class Solution:
    def findPeakElement(self, nums: List[int]) -> int:

        low = 0
        high = len(nums) - 1

        while low < high:
            mid = int(low - (low - high) / 2)
            if nums[mid] < nums[mid + 1]:
                low = mid + 1
            else:
                high = mid

        return low
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```