zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Linux常用命令 rm命令详解
    Python 面向对象(一)入门篇
    Python习题集(十六)
    Python 面向对象(三)公共变量,受保护变量,私有变量
    洛谷P1309 瑞士轮(归并排序)
    185. [USACO Oct08] 挖水井
    洛谷 P1078 文化之旅(CODEVS 1316)
    CODEVS 1074 食物链 2001年NOI全国竞赛(洛谷 P2024)
    186. [USACO Oct08] 牧场旅行
    157. [USACO Nov07] 奶牛跨栏
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8649343.html
Copyright © 2011-2022 走看看