【题目】
给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符:
‘A’ : Absent,缺勤
‘L’ : Late,迟到
‘P’ : Present,到场
如果一个学生的出勤记录中不超过一个’A’(缺勤)并且不超过两个连续的’L’(迟到),那么这个学生会被奖赏。
你需要根据这个学生的出勤记录判断他是否会被奖赏。
【示例 1】
输入: “PPALLP”
输出: True
【示例 2】
输入: “PPALLL”
输出: False
【代码】
【Python】
执行用时:
32 ms, 在所有 Python3 提交中击败了94.20%的用户
内存消耗:
14.8 MB, 在所有 Python3 提交中击败了29.68%的用户
class Solution:
def checkRecord(self, s: str) -> bool:
s+='0'
cntA=0
maxL=0
cnt=0
for x in s:
if x=='A':
cntA+=1
if x=="L":
cnt+=1
else:
maxL=max(maxL,cnt)
cnt=0
return cntA<=1 and maxL<=2
【Python find】
执行用时:
44 ms, 在所有 Python3 提交中击败了29.31%的用户
内存消耗:
14.8 MB, 在所有 Python3 提交中击败了28.71%的用户
class Solution:
def checkRecord(self, s: str) -> bool:
return (s.find('A')==-1 or s.find('A',s.find('A')+1)==-1) and (s.find("LLL")==-1)
# 没有A ==-1 ==-1
# 只有一个A !=-1 ==-1
# 2个以上A !=-1 !=-1