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. 将 Shiro 作为应用的权限基础 一:shiro的整体架构

    将 Shiro 作为应用的权限基础 一:shiro的整体架构 近来在做一个重量级的项目,其中权限.日志.报表.工作量由我负责,工作量还是蛮大的,不过想那么多干嘛,做就是了. 这段时间,接触的东西挺多, ...

  2. SQL注入之Sqli-labs系列第三篇

    废话不在多说  let's go!   继续挑战第三关(Error Based- String) 1.访问地址,加入参数后 and 1=1和and 1=2进行测试,木有任何动静 2.再使用 ' 出现报 ...

  3. Beta冲刺 第三天

    Beta冲刺 第三天 1. 昨天的困难 昨天的困难主要集中在对Ajax的使用上,不熟悉这种语法,所以也就浪费了时间,导致昨天的批量删除没有完全完成. 2. 今天解决的进度 潘伟靖: 1.完善了昨天没写 ...

  4. 201621123060《JAVA程序设计》第八周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 源代码: publ ...

  5. lambda及参数绑定

    一.介绍   对于STL中的算法,我们都可以传递任何类别的可调用对象.对于一个对象或一个表达式,如果可以对其使用调用运算符,则称它为可调用的.即,如果e是一个可调用的表达式,则我们可以编写代码e(ar ...

  6. 2017-2018-1 1623 bug终结者 冲刺007

    bug终结者 冲刺007 by 20162302 杨京典 今日任务:排行榜界面 排行榜界面,选项界面 简要说明 排行榜界面用于展示用户通关是所使用的步数和时间,选项界面可以调整背景音乐的开关.选择砖块 ...

  7. 【iOS】swift 枚举

    枚举语法 你可以用enum开始并且用大括号包含整个定义体来定义一个枚举: enum SomeEnumeration { // 在这里定义枚举 } 这里有一个例子,定义了一个包含四个方向的罗盘: enu ...

  8. Spring Boot jar包linux服务器部署

    Spring Boot 部署 一.使用命令行java -jar 常驻 nohup java -jar spring-boot-1.0-SNAPSHOT.jar > log.file 2>& ...

  9. 洛谷 U10783 名字被和谐了

    https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...

  10. 多种在线地图综合对比,Google,必应,arcgis Online...

    不同网络地图的对比 天地图 坐标系:WGS84 地图配色:   POI数量:丰富      有无建筑:有 地图特点:天地图按照国家标准进行配图,道路.水系.植被等图层用对应颜色渲染, POI信息丰富, ...