LeetCode Student Attendance Record I

LeetCode 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

给定一个学生出席字符串,P表示出席了,A表示未出席,L表示迟到。如果有超过1个A或者超过2个L,则该同学不能被评奖,否则可以。先给定一个字符串,问该同学能否被评奖。 简单题,一遍扫描,记录A的数量以及连续L的数量,根据规则判断就好。 代码如下: [cpp] class Solution { public: bool checkRecord(string s) { int n = s.size(); int A = 0, L = 0; for(size_t i = 0; i < n; ++i){ if(s[i] == ‘A’){ ++A; if(A > 1) return false; } else if(s[i] == ‘L’){ int tmp = 0; while(i < n && s[i] == ‘L’){ ++tmp; ++i; } –i; // caution L = max(tmp, L); if(L > 2) return false; } } return true; } }; [/cpp] 本代码提交AC,用时3MS。]]>

Leave a Reply

Your email address will not be published. Required fields are marked *