leetcode-master/problems/0941.有效的山脉数组.md

129 lines
4.0 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 align="center">
<a href="https://mp.weixin.qq.com/s/RsdcQ9umo09R6cfnwXZlrQ"><img src="https://img.shields.io/badge/PDF下载-代码随想录-blueviolet" alt=""></a>
<a href="https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw"><img src="https://img.shields.io/badge/刷题-微信群-green" alt=""></a>
<a href="https://space.bilibili.com/525438321"><img src="https://img.shields.io/badge/B站-代码随想录-orange" alt=""></a>
<a href="https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ"><img src="https://img.shields.io/badge/知识星球-代码随想录-blue" alt=""></a>
</p>
<p align="center"><strong>欢迎大家<a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
# 941.有效的山脉数组
题目链接https://leetcode-cn.com/problems/valid-mountain-array/
给定一个整数数组 arr如果它是有效的山脉数组就返回 true否则返回 false。
让我们回顾一下,如果 A 满足下述条件,那么它是一个山脉数组:
* arr.length >= 3
* 0 < i < arr.length - 1 条件下存在 i 使得
* arr[0] < arr[1] < ... arr[i-1] < arr[i]
* arr[i] > arr[i+1] > ... > arr[arr.length - 1]
![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210729103604.png)
示例 1
* 输入arr = [2,1]
* 输出false
示例 2
* 输入arr = [3,5,5]
* 输出false
示例 3
* 输入arr = [0,3,2,1]
* 输出true
# 思路
判断是山峰,主要就是要严格的保存左边到中间,和右边到中间是递增的。
这样可以使用两个指针left和right让其按照如下规则移动如图
<img src='https://code-thinking.cdn.bcebos.com/pics/941.有效的山脉数组.png' width=600> </img></div>
**注意这里还是有一些细节,例如如下两点:**
* 因为left和right是数组下表移动的过程中注意不要数组越界
* 如果left或者right没有移动说明是一个单调递增或者递减的数组依然不是山峰
C++代码如下:
```c++
class Solution {
public:
bool validMountainArray(vector<int>& A) {
if (A.size() < 3) return false;
int left = 0;
int right = A.size() - 1;
// 注意防止越界
while (left < A.size() - 1 && A[left] < A[left + 1]) left++;
// 注意防止越界
while (right > 0 && A[right] < A[right - 1]) right--;
// 如果left或者right都在起始位置说明不是山峰
if (left == right && left != 0 && right != A.size() - 1) return true;
return false;
}
};
```
如果想系统学一学双指针的话, 可以看一下这篇[双指针法:总结篇!](https://mp.weixin.qq.com/s/_p7grwjISfMh0U65uOyCjA)
# 其他语言版本
## Java
```java
class Solution {
public boolean validMountainArray(int[] arr) {
if (arr.length < 3) { // 此时,一定不是有效的山脉数组
return false;
}
// 双指针
int left = 0;
int right = arr.length - 1;
// 注意防止指针越界
while (left + 1 < arr.length && arr[left] < arr[left + 1]) {
left++;
}
// 注意防止指针越界
while (right > 0 && arr[right] < arr[right - 1]) {
right--;
}
// 如果left或者right都在起始位置说明不是山峰
if (left == right && left != 0 && right != arr.length - 1) {
return true;
}
return false;
}
}
```
## Python
```python
```
## Go
```go
```
## JavaScript
```js
```
-----------------------
* 作者微信[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)
* 知识星球[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码.jpg width=450> </img></div>