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
相关文章
相关标签/搜索