题解:
统计缺勤天数以及判断是否连续三天迟到即可
class Solution {
public:
bool checkRecord(string s) {
//要返回true:只能有一天缺勤,不能连续三天或三天以上迟到
//缺勤天数
int absent = 0;
for(int i = 0;i<s.size();i++){
if(s[i] == 'A'){
absent++;
if(absent>1){
return false;
}
}
if(s[i] == 'L'){
//如果前后两天都迟到 则返回false
if(i-1>=0 && s[i-1] == 'L' && i+1<s.size() && s[i+1] == 'L'){
return false;
}
}
}
return true;
}
};