diff options
author | Jack Sangdahl <[email protected]> | 2025-05-10 21:14:53 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-05-10 21:14:53 -0600 |
commit | eee47c29b5dd2a152a3c898525b8871503625a3e (patch) | |
tree | aedbeaad2de66b8250858038bed90ad1ecf00d4c | |
parent | 42c1b2150db0dfed4e106379f8ad6dc966bb3c2e (diff) | |
download | leetcode-eee47c29b5dd2a152a3c898525b8871503625a3e.tar.gz leetcode-eee47c29b5dd2a152a3c898525b8871503625a3e.zip |
1550. three consecutive odds
-rw-r--r-- | three_consecutive_odds.c | 8 | ||||
-rw-r--r-- | three_consecutive_odds.py | 10 |
2 files changed, 13 insertions, 5 deletions
diff --git a/three_consecutive_odds.c b/three_consecutive_odds.c index 1f65dd4..9885dd2 100644 --- a/three_consecutive_odds.c +++ b/three_consecutive_odds.c @@ -7,10 +7,10 @@ */ bool threeConsecutiveOdds(int *arr, int arrSize) { - int odds = 0; - for (int i = 0; i < arrSize && odds < 3; ++i) - odds = arr[i] % 2 ? odds + 1 : 0; - return odds == 3; + for (int i = 1; i < arrSize - 1; ++i) + if (1 & arr[i - 1] & arr[i] & arr[i + 1]) + return true; + return false; } int main() { diff --git a/three_consecutive_odds.py b/three_consecutive_odds.py index 62fafe3..7abb6bb 100644 --- a/three_consecutive_odds.py +++ b/three_consecutive_odds.py @@ -12,7 +12,15 @@ class Solution(object): :type arr: List[int] :rtype: bool """ - return "111" in "".join([str(i % 2) for i in arr]) + cnt = 0 + for i in arr: + if i % 2 != 0: + cnt += 1 + if cnt == 3: + return True + else: + cnt = 0 + return False if __name__ == "__main__": |