LeetCode 1550 Three Consecutive Odds Writeup

存在连续三个奇数的数组

原题

解法一

class Solution:
    def threeConsecutiveOdds(self, arr: List[int]) -> bool:
        pos = 0
        while pos + 2 < len(arr):
            if arr[pos] & 1 == 1 and arr[pos + 1] & 1 == 1 and arr[pos + 2] & 1 == 1:
                return True
            pos += 1
        return False

解法二

class Solution:
    def threeConsecutiveOdds(self, arr: List[int]) -> bool:
        pos = 0
        count = 0
        while pos <len(arr):
            if arr[pos] & 1 == 1:
                count += 1
            else:
                count = 0
            if count == 3:
                return True
            pos += 1
        return False

复杂度分析

  1. 空间复杂度:两种算法的复杂度都为O(n),后一种要稍好一些
  2. 空间复杂度:都为O(1)
上一篇:【Buuctf】[BJDCTF 2nd]elementmaster WriteUp


下一篇:buuoj Pwn writeup 76-80