给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:

  1. 'A' : Absent,缺勤
  2. 'L' : Late,迟到
  3. 'P' : Present,到场

如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。

你需要根据这个学生的出勤纪录判断他是否会被奖赏。

示例 1:

输入: "PPALLP"
输出: True

示例 2:

输入: "PPALLL"
输出: False

思路:暴力求解

class Solution:
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s.count('A') > 1:
            return False
        if 'LL' in s:
            i = 0
            while i < len(s):
                if s[i] == 'L':
                    if s[i:i+3].count('L') > 2:
                        return False
                i+=1
        return True

【Leetcode_总结】551. 学生出勤纪录 I - python

有些繁琐了,因为直接统计有没有‘LLL’即可

新代码:

class Solution:
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        if s.count('A') > 1:
            return False
        if 'LLL' in s:
            return False
        return True

【Leetcode_总结】551. 学生出勤纪录 I - python

相关文章:

  • 2021-12-10
  • 2021-12-14
  • 2021-12-08
  • 2021-04-08
  • 2021-05-30
  • 2021-09-06
  • 2021-12-24
  • 2021-10-23
猜你喜欢
  • 2021-11-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-30
  • 2021-07-27
  • 2022-01-04
相关资源
相似解决方案