解法一:
public class Solution {
//A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent)
//or more than two [continuous] 'L' (late). 对L的要求是[连续]超过2次
public boolean checkRecord(String s) {
int countA = 0;
int countL = 0;//这个代表最大的countL
int curCountL = 0;//这个代表当前连续的curCountL
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == 'A'){
countA++;
}
if(s.charAt(i) == 'L'){
curCountL++;
countL = Math.max(countL, curCountL);
} else{
curCountL = 0;
}
//System.out.println("countA:" + countA + " countL:" + countL + " curCountL:" + curCountL);
}
if(countA > 1 || countL > 2){
return false;
} else{
return true;
}
}
}
本文介绍了一种用于检查学生考勤记录是否符合奖励条件的算法。该算法通过遍历字符串来判断是否存在超过一次的缺席(A)或连续三次以上的迟到(L)情况。如果满足条件,则返回true,否则返回false。
558

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



