Skip to content

Commit c7a3dbe

Browse files
committed
Add leetcode problem 1550
1 parent 4e9bf06 commit c7a3dbe

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package leetcode
2+
3+
func threeConsecutiveOdds(arr []int) bool {
4+
cnt := 0
5+
for _, num := range arr {
6+
if num&1 == 1 {
7+
cnt++
8+
} else {
9+
cnt = 0
10+
}
11+
if cnt == 3 {
12+
return true
13+
}
14+
}
15+
return false
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn three_consecutive_odds(arr: Vec<i32>) -> bool {
5+
arr.windows(3).any(|x| x[0] & x[1] & x[2] & 1 == 1)
6+
}
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use super::*;
12+
13+
#[test]
14+
fn test() {
15+
let arr = vec![2, 6, 4, 1];
16+
assert!(!Solution::three_consecutive_odds(arr));
17+
18+
let arr = vec![1, 2, 34, 3, 4, 5, 7, 23, 12];
19+
assert!(Solution::three_consecutive_odds(arr));
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestThreeConsecutiveOdds(t *testing.T) {
11+
tests := []struct {
12+
arr []int
13+
14+
expected bool
15+
}{
16+
{[]int{2, 6, 4, 1}, false},
17+
{[]int{1, 2, 34, 3, 4, 5, 7, 23, 12}, true},
18+
}
19+
20+
for i, tt := range tests {
21+
tt := tt
22+
t.Run(fmt.Sprintf("Test %d", i), func(t *testing.T) {
23+
t.Parallel()
24+
actual := threeConsecutiveOdds(tt.arr)
25+
assert.Equal(t, tt.expected, actual)
26+
})
27+
}
28+
}

leetcode/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,3 +1415,6 @@ mod non_overlapping_intervals;
14151415

14161416
#[path = "00735_asteroid_collision/solution.rs"]
14171417
mod asteroid_collision;
1418+
1419+
#[path = "01550_three_consecutive_odds/solution.rs"]
1420+
mod three_consecutive_odds;

0 commit comments

Comments
 (0)