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

被折叠的 条评论
为什么被折叠?



