551. Student Attendance Record I

来源:互联网 发布:无人机航拍软件 编辑:程序博客网 时间:2024/06/05 19:41

551. Student Attendance Record I

Problem Description

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.Example 1:Input: "PPALLP"Output: TrueExample 2:Input: "PPALLL"Output: False

Implementation

class Solution {public:    bool checkRecord(string s) {        char rec = 0;        int s_len = s.size();        for(int idx = 0; idx < s_len; idx++) {            if(s[idx] == 'A') {                if(rec & 0xf0) {                    return false;                }                    else {                    rec = 0x80;                 }                }                else if(s[idx] == 'L') {                if(rec & 0x02) {                    return false;                }                    else {                    rec++;                }                }                else {                rec &= 0xf0;            }            }            return true;    }};