https://leetcode-cn.com/problems/student-attendance-record-i/
题目说要连续, 而且时间复杂度可满足 O(n^2), 我胆子就大了起来, 俩 for 循环一开, 👌。
先把 A 和 L 的数量设置好, 扫描字符串的时候挨个减。
但是要主要 L 的要求是要三个连续, 所以碰到 L 之后往后扫两位数看看 L 的个数, 如果不满足三个连续 L 的话, 再恢复 L = 3。
原来这种题目叫做模拟呀
class Solution:
def checkRecord(self, s: str) -> bool:
A, L = 1, 3
for i in range(len(s)):
if s[i] == 'A':
if A == 0:
return False
A -= 1
if s[i] == 'L':
if i + 2 < len(s):
for j in range(i, i + 3):
if s[j] == 'L':
L -= 1
if L <= 0:
return False
else:
L = 3
return True- 时间复杂度: O(n)
- 空间复杂度: O(1)
