LeetCode 1550. Three Consecutive Odds
Published in
Mar 14, 2021
題目:
給定一個array,如果有連續出現三個奇數,return True 其餘return False。
Example 1:
Input: arr = [2,6,4,1]
Output: false
Explanation: There are no three consecutive odds.
Example 2:
Input: arr = [1,2,34,3,4,5,7,23,12]
Output: true
Explanation: [5,7,23] are three consecutive odds.
思路:
- consecutive的話會利用一個count,每發現一個奇數就加一
奇偶數判斷 餘數為0或1 - count =0
for i in range(len(arr)):
if arr[i] % 2 == 1:
count +=1
if count ==3:
return True - else:
count = 0 重新算 - return False
Coding:
class Solution:
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
count=0
for i in range(len(arr)):
if arr[i] % 2 == 1:
count +=1
if count == 3:
return True
else:
count = 0
return False