题目描述:

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

要完成的函数:

bool checkRecord(string s)

说明:

1、这道题给定一个字符串,其中只有三种字符P/L/A,分别代表在场/迟到/缺席。如果一个学生出现了一次以上的缺席,或者连续两次以上的迟到,那么他就不能被奖励。要求判断是否某个学生能被奖励。

2、关于A的,很容易,遍历一遍字符串统计A出现次数,当次数大于1时,返回false,结束遍历。

关于L的,也不难,遍历一遍字符串,当碰到L时,判断下一个字符和再下一个字符是否均为L,如果满足,返回false,结束遍历(这里要注意边界条件,即下一个字符是否在字符串以内);如果不满足,那么继续处理下一个字符。

代码如下:

    bool checkRecord(string s) {int counta=0,countl=0;int s1=s.size();for(int i=0;i<s1;i++){if(s[i]=='A'){counta++;if(counta>1)return false;}else if(s[i+2]=='L'&&s[i+1]=='L'&&s[i]=='L'){if(i+2<s1)return false;}}return true;}

上述代码实测6ms,beats 70.11% of cpp submissions。

3、另一种方法

参考了讨论区的代码实现,发现了另一种实际花费时间更少的方法。

代码同样分享给大家,如下:

    bool checkRecord(string s) {int counta=0,countl=0;for(int i = 0;i < s.size();i++){if(s[i]=='A'){counta++;countl=0;//清空countl,重新开始if(counta>1)return false;}else if(s[i]=='L'){countl++;if(countl>2)return false;} else countl=0;}return true;}    

上述代码实测4ms,beats 100% of cpp submissions。

这样写代码看起来更加“清爽”,判断是否出现了连续的几个相同字符,采用的是碰到其他字符就“清空”的方法。

而2中的方法,是碰到‘L’时继续判断下一个以及再下一个字符是否仍是'L'的方式,这种方法不需要引进countl的频繁计算。

笔者还是更加喜欢“清爽”的代码,当L出现几百次才要return false的时候,明显清爽代码更省时间。

这道题目给予的启示是:当要判断字符是否连续出现时,可以采用“清空”的方法来做。

转载于:https://www.cnblogs.com/chenjx85/p/8984985.html

leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)相关推荐

  1. LeetCode 551. Student Attendance Record I

    题目: You are given a string representing an attendance record for a student. The record only contains ...

  2. 551. Student Attendance Record I -- Python

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

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

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

  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

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

  6. leetcode之Student Attendance Record I(551)

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

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

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

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

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

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

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

最新文章

  1. 人工智能及其应用(第5版).蔡自兴-2章课后习题。【参考答案】
  2. 你会为情怀买单么?反正我会!
  3. IDEA中Git操作
  4. sass笔记-1|Sass是如何帮你又快又好地搞定CSS的
  5. Qt3转Qt5(Ui部分)
  6. PythonGUI编程|使用Tkinter制作快递查询软件
  7. python怎么实现音乐快进_Python 超简单3行代码提取音乐高潮(附批量提取)
  8. 您不知道Bash:Bash阵列简介
  9. 设计模式 C++外观者模式
  10. ajax json node 布尔值_ajax和axios、fetch的区别
  11. JQuery怎么知道一个元素是否隐藏或显示How do you test if something is hidden in jQuery?
  12. 如果知道它们的热量,还想点吗?
  13. flash, sparkle, glow, gleam, twinkle, glitter
  14. 中国这个研究所有多牛?2021年发表431篇SCI,平均影响因子超过9,年度品种推广面积600余万亩.........
  15. Ubuntu中程序崩溃,杀死进程方法
  16. abs() 数字取绝对值
  17. partition 0 ended too near . couldnt find ntldr
  18. 大家一起学习用VBA查询数据
  19. 柳岩清晨晒素颜照称拿去辟邪 网友称吓人没认出来
  20. 华为 PIM-SM RP选举与切换

热门文章

  1. java正则表达式判断手机号_正则表达式学习之简单手机号和邮箱练习
  2. 查询去重_【Freya的MySQL课堂】DQL基础查询
  3. iOS持久化存储-CoreData简介
  4. Javascript-Switch
  5. openwrt lamp
  6. python selenium自动化(三)Chrome Webdriver的兼容
  7. 推荐:iReaper - MSDN中文Webcast下载程序
  8. MIME类型不可小视
  9. p字间距 html段落内文字设置字间距间隔
  10. React中级学习(第一天)