LeetCode #551 Student Attendance Record I 学生出勤记录 I

551 Student Attendance Record I 学生出勤记录 I

Description:
You are given a string representing an attendance record for a student. The record only contains the following three characters:
'A' : Absent.
'L' : Late.
'P' : Present.
A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).

You need to return whether the student could be rewarded according to his attendance record.

Example:

Example 1:
Input: "PPALLP"
Output: True

Example 2:
Input: "PPALLL"
Output: False

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

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

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

示例 :

示例 1:

输入: "PPALLP"
输出: True
示例 2:

输入: "PPALLL"
输出: False

思路:

扫描一遍找到 A的数量, 然后判断 'LLL'是否在字符串中即可
时间复杂度O(n), 空间复杂度O(1)

代码:
C++:

class Solution 
{
public:
    bool checkRecord(string s) 
    {
        int countA = 0;
        for (char c : s) 
        {
            if (c == 'A') countA++;
            if (countA > 1) return false;
        }
        return s.find("LLL") == s.npos;
    }
};

Java:

class Solution {
    public boolean checkRecord(String s) {
        int countA = 0;
        for (char c : s.toCharArray()) {
            if (c == 'A') countA++;
            if (countA > 1) return false;
        }
        return s.indexOf("LLL") == -1;
    }
}

Python:

class Solution:
    def checkRecord(self, s: str) -> bool:
        return 'LLL' not in s and s.count('A') < 2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容