mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
添加(0941.有效的山脉数组.md):增加typescript版本
This commit is contained in:
@ -157,6 +157,26 @@ var validMountainArray = function(arr) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function validMountainArray(arr: number[]): boolean {
|
||||||
|
const length: number = arr.length;
|
||||||
|
if (length < 3) return false;
|
||||||
|
let left: number = 0,
|
||||||
|
right: number = length - 1;
|
||||||
|
while (left < (length - 1) && arr[left] < arr[left + 1]) {
|
||||||
|
left++;
|
||||||
|
}
|
||||||
|
while (right > 0 && arr[right] < arr[right - 1]) {
|
||||||
|
right--;
|
||||||
|
}
|
||||||
|
if (left === right && left !== 0 && right !== length - 1)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user