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:數據結構

 

  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). ide

You need to return whether the student could be rewarded according to his attendance record.優化

Example 1:spa

Input: "PPALLP"
Output: True

 

Example 2:debug

Input: "PPALLL"
Output: False

 [暴力解法]:code

時間分析:blog

空間分析:three

 [優化後]:字符串

時間分析:

空間分析:

[奇葩輸出條件]:

[奇葩corner case]:

[思惟問題]:

覺得要用for 來循環找L,可是其實仍是index更方便

[一句話思路]:

String類的.indexof contains(雙引號字符串)法很方便也很基礎,要熟悉 多用

[輸入量]:空: 正常狀況:特大:特小:程序裏處理到的特殊狀況:異常狀況(不合法不合理的輸入):

[畫圖]:

[一刷]:

  1. 又錯了:布爾型默認狀況是return true, 通常狀況都是正常即正確

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分鐘肉眼debug的結果]:

[總結]:

String類的.indexof contains(雙引號字符串)方法很方便也很基礎

[複雜度]:Time complexity: O(n) Space complexity: O(1)

[英文數據結構或算法,爲何不用別的數據結構或算法]:

[關鍵模板化代碼]:

[其餘解法]:

[Follow Up]:

[LC給出的題目變變變]:

552. Student Attendance Record II 具體方案還用DP就不懂了

 [代碼風格] :

class Solution {
    public boolean checkRecord(String s) {
        //cc
        if (s.length() == 0) {
            return true;
        }
        
        //judge
            if ((s.indexOf("A") != s.lastIndexOf("A")) || (s.contains("LLL"))) return false;
            
        //return 
        return true;
    }
}
View Code
相關文章
相關標籤/搜索