题目:
解答:
依次扫描字符串,并用两个计数器记录A和L的数量,其中对于L计数器,因为需要是连续计数,所以如果碰到A和P,则需要重置L计数器。
当A计数器 > 1 时,直接返回false。当L计数器 > 2 时,直接返回false。
都扫描结束,则返回true。
1 class Solution { 2 public: 3 bool checkRecord(string s) 4 { 5 int countA = 0; 6 int countL = 0; 7 for (int i = 0; i < s.length(); i++) 8 { 9 char c = s[i]; 10 if (c == ‘P‘) 11 { 12 countL = 0; 13 } 14 else 15 { 16 if (c == ‘A‘) 17 { 18 countA++; 19 if (countA > 1) 20 { 21 return false; 22 }; 23 countL = 0; 24 } 25 else if (c == ‘L‘) 26 { 27 countL++; 28 if (countL > 2) 29 { 30 return false; 31 }; 32 }; 33 }; 34 }; 35 return true; 36 } 37 };
原文:https://www.cnblogs.com/ocpc/p/12827394.html