551. 学生出勤记录 I

题目:给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符:'A' : Absent,缺勤;'L' : Late,迟到;'P' : Present,到场
代码:class Solution {
public boolean checkRecord(String s) {
int countA = 0,countL = 0;
for(char c:s.toCharArray()){
if(c'A'){
countA++;
countL=0;
} else if(c
'L'){
countL++;
} else{
countL=0;
}
if(countA>1){
return false;
}
if(countL>2){
return false;
}
}
return true;
}
}
注意:可以使用s.indexOf("LLL")<0判断是否出现过LLL,可使用正则表达式做:!s.matches(".(A.A|LLL).*");(使用正则效率较慢)

posted @ 2020-11-11 11:15  for_ward  阅读(80)  评论(0)    收藏  举报