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 79fdb21

Browse files
committed
Add: Max Consecutive Ones
1 parent 5af694b commit 79fdb21

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
package max_consecutive_ones
2+
3+
func findMaxConsecutiveOnes(nums []int) int {
4+
ans, count := 0, 0
5+
for _, v := range nums {
6+
if v == 1 {
7+
count++
8+
if count > ans {
9+
ans = count
10+
}
11+
} else {
12+
count = 0
13+
}
14+
}
15+
return ans
16+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,27 @@
11
package max_consecutive_ones
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected int
8+
}
9+
10+
func TestFindMaxConsecutiveOnes(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 1, 0, 1, 1, 1},
14+
expected: 3,
15+
},
16+
{
17+
input: []int{1, 0, 1, 1, 0, 1},
18+
expected: 2,
19+
},
20+
}
21+
for _, tc := range tests {
22+
output := findMaxConsecutiveOnes(tc.input)
23+
if output != tc.expected {
24+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
25+
}
26+
}
27+
}

0 commit comments

Comments
(0)

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