[LeetCode]551. Student Attendance Record I

来源:互联网 发布:网络防御系统 编辑:程序博客网 时间:2024/05/16 10:12

[LeetCode]551. Student Attendance Record I

题目描述

这里写图片描述

思路

水题,扫描一次,统计A和连续出现L的次数,不符合要求返回false即可

代码

#include <iostream>#include <string>using namespace std;class Solution {public:    bool checkRecord(string s) {        int countA = 0, checkL = 0;        for (char ch : s) {            if (ch == 'A')                ++countA;            if (ch == 'L')                ++checkL;            if (ch != 'L')                checkL = 0;            if (countA > 1 || checkL > 2)                return false;        }        return true;    }};int main() {    Solution s;    cout << s.checkRecord("PPALLP") << endl;    cout << s.checkRecord("PPALLL") << endl;    system("pause");    return 0;}
0 0
原创粉丝点击