[Leetcode] 551. Student Attendance Record I 解题报告

来源:互联网 发布:arm linux gcc gnueabi 编辑:程序博客网 时间:2024/06/05 14:13

题目

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

思路

练手题目。咋感觉这道题目在原来出现过?

代码

class Solution {public:    bool checkRecord(string s) {        int max_absent = 0, max_late = 0, index = 0;        while (index < s.length()) {            if (s[index] == 'L') {                int late = 1;                while (++index < s.length() && s[index] == 'L') {                    ++late;                }                max_late = max(max_late, late);            }            else {                max_absent += s[index] == 'A' ? 1 : 0;                ++index;            }        }        return max_absent <= 1 && max_late <= 2;    }};

阅读全文
0 0
原创粉丝点击