[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"));
    }
};

 

參考資料:

https://discuss.leetcode.com/topic/86651/c-1-liner

https://discuss.leetcode.com/topic/86571/one-line-java-mixed-solution

https://discuss.leetcode.com/topic/86534/tiny-ruby-short-python-java-c/2

 

LeetCode All in One 題目講解匯總(持續更新中...)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM