8000 :sparkles: Added solution to 941 · ninefive/awesome-golang-leetcode@2dcab61 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2dcab61

Browse files
author
Sathish Babu
committed
✨ Added solution to 941
1 parent 78fa867 commit 2dcab61

File tree

3 files changed

+45
-8
lines changed

3 files changed

+45
-8
lines changed

src/0941.Valid-Mountain-Array/README.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,35 @@
55
66
## Description
77

8+
Given an array A of integers, return true if and only if it is a valid mountain array.
9+
10+
Recall that A is a mountain array if and only if:
11+
12+
A.length >= 3
13+
There exists some i with 0 < i < A.length - 1 such that:
14+
A[0] < A[1] < ... A[i-1] < A[i]
15+
A[i] > A[i+1] > ... > A[A.length - 1]
16+
17+
818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: [2,1]
22+
Output: false
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: [3,5,5]
29+
Output: false
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: [0,3,2,1]
36+
Output: true
1337
```
1438

1539
## 题意
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(A []int) bool {
4+
n := len(A)
5+
if n <= 2 {
6+
return false
7+
}
8+
pass := 0
9+
for ; pass+1 < n && A[pass] < A[pass+1]; pass++ {
10+
}
11+
if pass == 0 || pass == n-1 {
12+
return false
13+
}
14+
for ; pass+1 < n && A[pass] > A[pass+1]; pass++ {
15+
}
16+
return pass == n-1
517
}

src/0941.Valid-Mountain-Array/Solution_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs []int
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase", []int{2, 1}, false},
17+
{"TestCase", []int{3, 5, 5}, false},
18+
{"TestCase", []int{3, 4, 5}, false},
19+
{"TestCase", []int{0, 3, 2, 1}, true},
1920
}
2021

2122
// 开始测试

0 commit comments

Comments
 (0)
0