aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--three_consecutive_odds.c8
-rw-r--r--three_consecutive_odds.py10
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__":