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

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

 [暴力解法]:

时间分析:

空间分析:

 [优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩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

 

posted @ 2018-03-26 10:35  苗妙苗  阅读(118)  评论(0编辑  收藏  举报