题目:
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次),连续迟到次数不超过2次(大于2次),其中‘A’表示缺席,‘L’表示迟到,’P’表示出席。具体思路见注释。

代码:

class Solution {
public:bool checkRecord(string s) {int lens=s.length();if(lens<=1){//如果s的长度小于等于1,直接返回truereturn true;}unordered_map<char,int> res{{'A',0},{'L',0}};//保存A的数和连续L数for(int i=0;i<lens;++i){if(s[i]=='P'){//如果遇到P,将res中L对应的值清零res['L']=0;}if(s[i]=='A'){//如果遇到A++res['A'];//res中A对应的计数加1,并且将L对应的值清零res['L']=0;}if(s[i]=='L'){//如果遇到L,res中对应的值加1++res['L'];if(res['L']>2){//如果res中的L的值大于2了,也就是有连续3个L了,返回falsereturn false;}}}return res['A']<=1;//最后判定res中A的值如果小于等于1,返回true;否则返回false(迟到次数大于1)}
};

输出结果: 6ms

LeetCode 551. Student Attendance Record I相关推荐

  1. 551. Student Attendance Record I -- Python

    551. Student Attendance Record I You are given a string representing an attendance record for a stud ...

  2. 551. Student Attendance Record I 从字符串判断学生考勤

    [抄题]: You are given a string representing an attendance record for a student. The record only contai ...

  3. leetCode题解 Student Attendance Record I

    1.题目描述 You are given a string representing an attendance record for a student. The record only conta ...

  4. 【LeetCode】Student Attendance Record I记录学生出勤率

    问题描述 You are given a string representing an attendance record for a student. The record only contain ...

  5. leetcode之Student Attendance Record I(551)

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

  6. LeetCode——552. 学生出勤记录 II(Student Attendance Record II)[困难]——分析及代码(Java)

    LeetCode--552. 学生出勤记录 II[Student Attendance Record II][困难]--分析及代码[Java] 一.题目 二.分析及代码 1. 动态规划 (1)思路 ( ...

  7. 552. Student Attendance Record II(学生出勤记录 II)

    552. Student Attendance Record II 博主看到这题时候第一感觉就是DP方法,但是想了一会方程式写不出来,然后开始怀疑是不是动态方程,想了一下过程感觉极其复杂.看了讨论区, ...

  8. C#LeetCode刷题之#551-学生出勤纪录 I​​​​​​​(Student Attendance Record I)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3953 访问. 给定一个字符串来代表一个学生的出勤纪录,这个纪录仅 ...

  9. [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. wordpress 后台,登录,注册开启https的重写规则
  2. 二维数组更改vue_使用vue中的v-for遍历二维数组的方法
  3. 93%程序员都做过的事:来写一个python式的hello world吧
  4. jvm学习笔记(2)——java对象的内存布局
  5. 如何在github上下载某个项目的单独某个目录
  6. echo 单引号和双引号
  7. recyclervie刷新到底部_自定义RecyclerView添加HeaderView,添加FooterView,实现滑动到底部,加载更多...
  8. 我需要具备的知识和能力总结
  9. android接收串口发送字符,android发送接收uart串口代码 - 源码下载|通讯/手机编程|android开发|源代码 - 源码中国...
  10. java中this有什么作用_Java中this有什么用
  11. 社交巨人屏蔽Google搜索 Facebook发展强劲拟上市
  12. 荐书一本-----《天才在左,疯子在右》
  13. kaggle员工离职预测案例(1)
  14. opencv-python 中文显示在图像上
  15. Python爬取酷狗音乐歌手信息
  16. PHP上传用户头像及头像的缩略图
  17. P2197 nim博弈
  18. java excel 列_java 如何操作excel 插入一列
  19. Android 平台应用使用RxAndroid
  20. ArchLinux安装fcitx5以及拼音输入法

热门文章

  1. 用python实现yale人脸数据集降维_用Python实现磁盘IO操作全攻略,让数据流动起来!...
  2. h5键盘把页面顶走了_微信h5开发的坑
  3. java onchange_jsp中select的onchange事件用法实例
  4. 开启防火墙并添加出入站规则
  5. ubuntun 16.04环境安装Caffe过程
  6. heatmap个人简单理解
  7. Array,List,数据类型,caseclss模式匹配
  8. 抓住那只喵(HTML5-神经猫)
  9. [弹性力学]应力转轴公式和应变转轴公式的展开式
  10. python关闭文件的函数_python打开与关闭文档