↓快轉到主要內容
  1. LeetCode/

LeetCode 1550: Three Consecutive Odds

·1 分鐘· ·
LeetCode Easy Array
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: easy 第一次嘗試: 2025-05-11

  • 總花費時間:50:00.00

解題思路
#

設定一個計數器來紀錄連續的奇數個數。
當計數達到 3,就可以提前回傳結果為 True。

解法
#

class Solution:
    def threeConsecutiveOdds(self, arr: List[int]) -> bool:
        
        count = 0
        for num in arr:
            if num%2:
                count+=1
            else:
                count = 0

            if count == 3:
                return True

        return False

收穫
#

遇到的問題
#

相關文章

LeetCode 1920: Build Array from Permutation
·1 分鐘
LeetCode Easy Array Simulation
LeetCode 1920 解題筆記:照定義 ans[i] = nums[nums[i]];要 O(1) 額外空間時,把新值乘 n 疊在原本的值上
LeetCode 2176: Count Equal and Divisible Pairs in an Array
·1 分鐘
LeetCode Easy Array
LeetCode 2176 解題筆記:n 只有 100,枚舉所有配對;條件是值相等而且 index 的乘積 i * j 能被 k 整除
LeetCode 3507: Minimum Pair Removal to Sort Array I
·1 分鐘
LeetCode Easy Simulation Array
LeetCode 3507 解題筆記:n 很小直接模擬,切片賦值右邊要是 list
LeetCode 1431: Kids With the Greatest Number of Candies
·1 分鐘
LeetCode Easy Array
LeetCode 1431 解題筆記:先算一次最大值,每個人加上額外的糖果後 >= 最大值就是 True;平手也算最多
LeetCode 3487: Maximum Unique Subarray Sum After Deletion
·1 分鐘
LeetCode Easy Greedy Hash-Table Array
LeetCode 3487 解題筆記:可以任意刪除,所以等於挑不重複的值;非負的全拿,全是負數時只拿最大的一個
LeetCode 3131: Find the Integer Added to Array I
·1 分鐘
LeetCode Easy Array
LeetCode 3131 解題筆記:每個數都加上同一個 x,所以 x 就是兩個陣列最小值的差(或總和的差除以長度)