方法一:
暴力:
class Solution {
public:
bool checkRecord(string s) {
int Absent=0,Late=0;
for(int i=0;i<s.size();i++){
if(s[i]=='A'){
Absent++;
if(Absent>1){
return false;
}
}
}
if(s.size()<3){
return true;
}
for(int i=0;i<s.size()-2;i++){
if(s[i]=='L'&&s[i+1]=='L'&&s[i+2]=='L'){
return false;
}
}
return true;
}
};
方法二:
一次循环:
class Solution {
public:
bool checkRecord(string s) {
int Absent=0,Late=0;
s+="PP";
for(int i=0;i<s.size();i++){
if(s[i]=='A'){
Absent++;
if(Absent>1){
return false;
}
}
if(s[i]=='L'&&s[i+1]=='L'&&s[i+2]=='L'){
return false;
}
}
return true;
}
};