3206.交替组I
链接:3206.交替组I
难度:Easy
标签:数组、滑动窗口
简介:请你返回 交替 组的数目。
题解 1 - python
- 编辑时间:2024-11-26
- 内存消耗:17.32MB
- 编程语言:python
- 解法介绍:bfs
class Solution:
    def numberOfAlternatingGroups(self, colors: List[int]) -> int:
        n = len(colors)
        p1 = colors[0]
        p2 = colors[1]
        i = 2
        res = 0
        while True:
            p3 = colors[i]
            if p1 == p3 and p1 != p2:
                res += 1
            p1 = p2
            p2 = p3
            i = (i + 1) % n
            if i == 2: break
        return res