Monday, November 26, 2018

[LeetCode]Student Attendance Record I

这道题比较简单,按照题目要求来即可,线性扫一遍,时间复杂度O(N),空间复杂度O(1),代码如下:


class Solution {
public:
bool checkRecord(string s) {
int len = s.size(), cntA = 0, i = 0;
while(i < len)
{
char c = s[i];
if(c == 'A')
{
if(++cntA > 1)return false;
}
else if(c == 'L')
{
int j = i;
while(j < len && s[j] == 'L')++j;
if(j - i > 2)return false;
i = j;
continue;
}
++i;
}
return true;
}
};

No comments:

Post a Comment