题目
You are given a string representing an attendance record for a student. The record only contains the following three characters:
1.’A’ : Absent.
2.’L’ : Late.
3.’P’ : Present.
A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).
You need to return whether the student could be rewarded according to his attendance record.
Example1:
Input: "PPALLP"
Output: True
Example2:
Input: "PPALLL"
Output: False
思路
该题十分简单。遍历字符串,进行两种操作:
1.对字符串中的字符A计数,出现两个以上A就返回false。
2.对字符串中的连续L计数,连续两个以上L返回false。
代码
class Solution {
public:
bool checkRecord(string s) {
int n=0,m=0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='A')
{
n++;
m=0;
if(n>1)
return false;
}
else if(s[i]=='L')
{
m++;
if(m>2)
return false;
}
else
{
m=0;
}
}
return true;
}
};