Title:Student Attendance Record I 551
Difficulty:Easy
原题leetcode地址:https://leetcode.com/problems/student-attendance-record-i/
1. 注解见代码注释
时间复杂度:O(n),两次一层for循环。
空间复杂度:O(1),没有申请额外空间。
/**
* 先判断A,再判断L
* @param s
* @return
*/
public static boolean checkRecord(String s) {
int aCount = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
aCount++;
}
if (aCount >= 2) {
return false;
}
}
for (int i = 0; i < s.length() - 2; i++) {
if (s.charAt(i) == 'L' && s.charAt(i + 1) == 'L' && s.charAt(i + 2) == 'L') {
return false;
}
}
return true;
}