Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0037025

Browse files
author
Sandy
authored
Merge pull request #202 from openset/develop
Add: Valid Mountain Array
2 parents 49cf209 + ae8ff5f commit 0037025

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
11
package valid_mountain_array
2+
3+
func validMountainArray(A []int) bool {
4+
flag := false
5+
if len(A) < 3 {
6+
return false
7+
}
8+
for i, v := range A[1:] {
9+
if !flag && v <= A[i] {
10+
if flag = true; i == 0 {
11+
return false
12+
}
13+
}
14+
if flag && v >= A[i] {
15+
return false
16+
}
17+
}
18+
return flag
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
package valid_mountain_array
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected bool
8+
}
9+
10+
func TestValidMountainArray(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{2, 1},
14+
expected: false,
15+
},
16+
{
17+
input: []int{3, 5, 5},
18+
expected: false,
19+
},
20+
{
21+
input: []int{0, 3, 2, 1},
22+
expected: true,
23+
},
24+
{
25+
input: []int{1, 2, 3, 2},
26+
expected: true,
27+
},
28+
{
29+
input: []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
30+
expected: false,
31+
},
32+
{
33+
input: []int{1, 2, 3, 4, 5},
34+
expected: false,
35+
},
36+
}
37+
for _, tc := range tests {
38+
output := validMountainArray(tc.input)
39+
if output != tc.expected {
40+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /