1. 程式人生 > >[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

這道題讓我們判斷學生的出勤率是否是優秀,判斷標準是不能缺勤兩次和不能連續遲到三次,那麼最直接的方法就是分別記錄缺勤和連續遲到的次數,如果當前遇到缺勤,那麼缺勤計數器自增1,如果此時次數大於1了,說明已經不是優秀了,直接返回false,否則連續遲到計數器清零。如果當前遇到遲到,那麼連續遲到計數器自增1,如果此時連續遲到計數器大於1了,說明已經不是優秀了,直接返回false。如果遇到正常出勤了,那麼連續遲到計數器清零,參見程式碼如下:

解法一:

class Solution {
public:
    bool checkRecord(string s) {
        int cntA = 0, cntL = 0;
        for (char c : s) {
            if (c == 'A') {
                if (++cntA > 1) return false;
                cntL = 0;
            } else if (c == 'L') {
                if (++cntL > 2
) return false; } else { cntL = 0; } } return true; } };

那麼這種方法利用到了string的查詢函式,由於本題的邏輯並不複雜,所以我們可以直接對字串進行操作,利用STL提供的find函式,方法是同時滿足下面兩個條件就是優秀,第一個條件是找不到A,或者正著找A和逆著找A在同一個位置(說明只有一個A);第二個條件是找不到LLL,說明不能連續遲到三次,參見程式碼如下:

解法二:

class Solution {
public:
    bool checkRecord(string s) {
        return (s.find("A") == string::npos || s.find("A") == s.rfind("A")) && s.find("LLL") == string::npos;
    }
};

再來看使用正則匹配來做的解法,我們找出不合題意的情況,然後取反即可,正則匹配式是A.*A|LLL,其中.*表示有零個或者多個,那麼A.*A就是至少有兩A的情況,LLL是三個連續的遲到,|表示兩個是或的關係,只要能匹配出任意一種情況,就會返回false,參見程式碼如下:

解法三:

class Solution {
public:
    bool checkRecord(string s) {
        return !regex_search(s, regex("A.*A|LLL"));
    }
};

參考資料: