LeetCode 551. 学生出勤纪录 I(C、python)

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

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

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

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

示例 1:

输入: "PPALLP"
输出: True

示例 2:

输入: "PPALLL"
输出: False

C语言

bool checkRecord(char* s) 
{
    int n=strlen(s);
    int countA=0;
    int countL=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='A')
        {
            countA++;
            if(countA>1)
            {
                return false;
            }
        }
        if(s[i]=='L')
        {
            countL++;
            if(countL>2)
            {
                return false;
            }
        }
        if(s[i]!='L')
        {
            countL=0;
        }
    }
    return true;
}

python

class Solution:
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        n=len(s)
        countA=0
        countL=0
        for i in range(0,n):
            if s[i]=='A':
                countA += 1
                if countA>1:
                    return False
            if s[i]=='L':
                countL += 1
                if countL>2:
                    return False
            if s[i]!='L':
                countL = 0
        return True
        

猜你喜欢

转载自blog.csdn.net/qq_27060423/article/details/82719683