551. Student Attendance Record I

来源:互联网 发布:淘宝卖假货封店 编辑:程序博客网 时间:2024/06/04 19:52

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

一开始我以为这道题很简单,就是判断是否出现多于一个A或者多于两个L,当我提交代码的时候,我发觉原来我做错了,我忽略题目two continuous 'L' (late).中的continuous,才知道事情原来没有那么简单,最后改了一下代码,发觉事情也很简单。

AC:

class Solution {public:    bool checkRecord(string s) {        int lengths = s.length();        int countA = 0;        int countL = 0;        for (int i = 0; i < lengths; i++)        {            if(s[i] == 'A')            countA++;            if(i>=2){            if(s[i] == 'L'&&s[i-1]=='L'&&s[i-2]=='L')            return false;            }        }        if(countA>1)        return false;        return true;    }};


原创粉丝点击