[Leetcode] 551. Student Attendance Record I 解题报告

这篇博客讨论了LeetCode的第551题,学生出勤记录I。题目要求根据学生的出勤记录字符串判断他们是否可以获得奖励。学生在记录中最多只能有一个'A'(缺席)或连续两个'L'(迟到)。博客作者提供了问题的思路和解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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 1:

Input: "PPALLP"
Output: True

Example 2:

Input: "PPALLL"
Output: False

思路

练手题目。咋感觉这道题目在原来出现过?

代码

class Solution {
public:
    bool checkRecord(string s) {
        int max_absent = 0, max_late = 0, index = 0;
        while (index < s.length()) {
            if (s[index] == 'L') {
                int late = 1;
                while (++index < s.length() && s[index] == 'L') {
                    ++late;
                }
                max_late = max(max_late, late);
            }
            else {
                max_absent += s[index] == 'A' ? 1 : 0;
                ++index;
            }
        }
        return max_absent <= 1 && max_late <= 2;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值