Leetcode 551. Student Attendance Record I

題目描述:

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.

這道題目十分簡(jiǎn)單,最直觀的解法就是對(duì)A和L的次數(shù)記錄下來(lái),然后要是超出就返回false。但是我采取的解法是使用向量機(jī)(向量機(jī)在處理字符串問(wèn)題上十分好用

bool checkRecord(string s) {
        vector<vector<int>> vm= {
            {0,1,3},    //當(dāng)前位置的前一個(gè)位置不為L(zhǎng),且沒(méi)有A的狀態(tài)
            {0,2,3},    // 當(dāng)前位置的前一個(gè)位置時(shí)L,且沒(méi)有A的狀態(tài)
            {0,-1,3},   // 當(dāng)前位置的前兩個(gè)位置都是L,且沒(méi)有A的狀態(tài)
            {3,4,-1},   // 當(dāng)前位置的前一個(gè)位置不為L(zhǎng),且有一個(gè)A的狀態(tài)
            {3,5,-1},   // 當(dāng)前位置的前一個(gè)位置為L(zhǎng),且有一個(gè)A的狀態(tài)
            {3,-1,-1}   // 當(dāng)前位置的前兩個(gè)位置為L(zhǎng),且有一個(gè)A的狀態(tài)
        };
        int currentState = 0;
        for(int i = 0;i < s.size();i++){
            if(s[i] == 'P'){
                currentState = vm[currentState][0];
            }else if(s[i] == 'L'){
                currentState = vm[currentState][1];
            }else if(s[i] == 'A'){
                currentState = vm[currentState][2];
            }
            if(currentState == -1){
                return false;
            }
        }
        return true;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容