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

这道题让我们判断学生的出勤率是否是优秀,判断标准是不能缺勤两次和不能连续迟到三次,那么最直接的方法就是分别记录缺勤和连续迟到的次数,如果当前遇到缺勤,那么缺勤计数器自增1,如果此时次数大于1了,说明已经不是优秀了,直接返回false,否则连续迟到计数器清零。如果当前遇到迟到,那么连续迟到计数器自增1,如果此时连续迟到计数器大于1了,说明已经不是优秀了,直接返回false。如果遇到正常出勤了,那么连续迟到计数器清零,参见代码如下:

解法一:

class Solution {
public:
bool checkRecord(string s) {
int cntA = , cntL = ;
for (char c : s) {
if (c == 'A') {
if (++cntA > ) return false;
cntL = ;
} else if (c == 'L') {
if (++cntL > ) return false;
} else {
cntL = ;
}
}
return true;
}
};

那么这种方法利用到了string的查找函数,由于本题的逻辑并不复杂,所以我们可以直接对字符串进行操作,利用STL提供的find函数,方法是同时满足下面两个条件就是优秀,第一个条件是找不到A,或者正着找A和逆着找A在同一个位置(说明只有一个A);第二个条件是找不到LLL,说明不能连续迟到三次,参见代码如下:

解法二:

class Solution {
public:
bool checkRecord(string s) {
return (s.find("A") == string::npos || s.find("A") == s.rfind("A")) && s.find("LLL") == string::npos;
}
};

再来看使用正则匹配来做的解法,我们找出不合题意的情况,然后取反即可,正则匹配式是A.*A|LLL,其中.*表示有零个或者多个,那么A.*A就是至少有两A的情况,LLL是三个连续的迟到,|表示两个是或的关系,只要能匹配出任意一种情况,就会返回false,参见代码如下:

解法三:

class Solution {
public:
bool checkRecord(string s) {
return !regex_search(s, regex("A.*A|LLL"));
}
};

参考资料:

https://discuss.leetcode.com/topic/86651/c-1-liner

https://discuss.leetcode.com/topic/86571/one-line-java-mixed-solution

https://discuss.leetcode.com/topic/86534/tiny-ruby-short-python-java-c/2

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Student Attendance Record I 学生出勤记录之一的更多相关文章

  1. [LeetCode] Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  2. [LeetCode] 552. Student Attendance Record II 学生出勤记录之二

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  3. 552 Student Attendance Record II 学生出勤记录 II

    给定一个正整数 n,返回长度为 n 的所有可被视为可奖励的出勤记录的数量. 答案可能非常大,你只需返回结果mod 109 + 7的值.学生出勤记录是只包含以下三个字符的字符串:    1.'A' : ...

  4. Leetcode551.Student Attendance Record I学生出勤记录1

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符: 'A' : Absent,缺勤 'L' : Late,迟到 'P' : Present,到场 如果一个学生的出勤纪录中不超过一个' ...

  5. LeetCode 551. Student Attendance Record I (学生出勤纪录 I)

    You are given a string representing an attendance record for a student. The record only contains the ...

  6. 551 Student Attendance Record I 学生出勤纪录 I

    给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:    'A' : Absent,缺勤    'L' : Late,迟到    'P' : Present,到场如果一个学生的出勤纪 ...

  7. LeetCode Student Attendance Record I

    原题链接在这里:https://leetcode.com/problems/student-attendance-record-i/description/ 题目: You are given a s ...

  8. [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II

    Given a positive integer n, return the number of all possible attendance records with length n, whic ...

  9. LeetCode算法题-Student Attendance Record I(Java实现)

    这是悦乐书的第258次更新,第271篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第125题(顺位题号是551).您将获得一个表示学生出勤记录的字符串. 该记录仅包含以下 ...

随机推荐

  1. 爬虫(requests)

    requests库包含两个对象:Response和Requests  Response对象属性:    r.status_code    HTTP请求的返回状态,200表示成功 r.text     ...

  2. VS2013创建Windows服务 || VS2015+Windows服务简易教程

    转自:https://www.cnblogs.com/no27/p/4849123.htmlhttps://blog.csdn.net/ly416/article/details/78860522 V ...

  3. Linux下的进程与线程(一)—— 进程概览

    进程是操作系统分配资源的基本单位.线程是操作系统进行运行和调度的基本单位. 进程之间可以切换,以便轮流占用CPU,实现并发.一般进程运行在用户模式下,只能执行指令集中的部分指令. 当进程进行上下文切换 ...

  4. MySQL的学习记录(3.31更新)

    MySQL的学习记录(3.31更新) 0x00 安装及配置 Windows 1.首先官网下载(https://dev.mysql.com/downloads/mysql/) ps:不想官网下载的可以到 ...

  5. (译文)学习ES6非常棒的特性-深入研究var, let and const

    Var var firstVar; //firstVar被声明,它的默认值是undefined var secondVar = 2; //secondVar被声明,被赋值2 先看一个例子: var i ...

  6. C语言第五次博客作业

    一.PTA实验作业 题目1:6-6 使用函数输出水仙花数 1. 本题PTA提交列表 2. 设计思路 (1) 首先先定义narcissistic函数. (2)定义四个整形变量n,a,d,cnt,sum, ...

  7. 项目Alpha冲刺Day12

    一.会议照片 二.项目进展 1.今日安排 修复全局的日期转换问题,完成用户所有相关的模块,对全局的异常处理做优化.其他模块进行一部分实现. 2.问题困难 全局异常处理后发现没有进行按照链进行下去,造成 ...

  8. APP的案例分析-美团外卖

    大一才开始用软件订外卖了,很方便  ,上手快只要注册个账号登陆即可,支付时自动跳转到其他支付应用.严重的bug也没有,只有之前一段时间通过首单可以刷优惠,之后也修复了. 身边的同学也很多都在用.方便省 ...

  9. MySQL 服务安装及命令使用

    MySQL 服务安装及命令使用 课程来源说明 本节实验后续至第17节实验为本课程的进阶篇,都基于 MySQL 官方参考手册制作,并根据实验楼环境进行测试调整改编.在此感谢 MySQL 的开发者,官方文 ...

  10. java从网络中下载图片到本地

    public class imageDownload { public static void main(String[] args) { String url = "http://loca ...