Student Attendance Record I

来源:互联网 发布:centos如何安装rpm包 编辑:程序博客网 时间:2024/06/01 07:11

Student Attendance Record I

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.

Example 1:

Input: "PPALLP"Output: True

Example 2:

Input: "PPALLL"Output: False
解析:求absent的个数,和最大连续late的个数,若absent大于1,或者最大连续late大于2返回false

代码:

class Solution {public:    bool checkRecord(string s) {        int abnum=0;        int latenum=0;        int lastnum=0;        for (int i=0; i<s.size(); i++)        {            if (s[i]=='A')            {                 abnum++;                 lastnum=max(lastnum,latenum);                 latenum=0;            }            else if (s[i]=='L')            {                 latenum++;            }            else            {                lastnum=max(lastnum,latenum);                latenum=0;            }                  }                lastnum=max(lastnum,latenum);        if (abnum>1||lastnum>2)        return false;        return true;            }};




0 0