551. Student Attendance Record I

来源:互联网 发布:格林数据 编辑:程序博客网 时间:2024/06/05 17:19

You are given a string representing an attendance record for a student. The record only contains the following three characters:

‘A’ : Absent.
‘L’ : Late.
‘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.

这道题就是注意随时监测,如果A超了1,L超了连续的2就马上return,注意L是连续的,读题要注意。
代码如下:

class Solution {public:    bool checkRecord(string s) {        bool flag=true;        int a[2];        for(int i=0;i<2;i++){            a[i]=0;        }        for(int i=0;i<s.size();i++){            if(s[i]=='A'){                a[1]=0;                a[0]++;                if(a[0]>1)return false;            }            else if(s[i]=='L'){                a[1]++;                if(a[1]>2)return false;            }            else{                a[1]=0;            }        }       return true;    }};
0 0
原创粉丝点击