题目描述:

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=,countl=;
int s1=s.size();
for(int i=;i<s1;i++)
{
if(s[i]=='A')
{
counta++;
if(counta>)
return false;
}
else if(s[i+]=='L'&&s[i+]=='L'&&s[i]=='L')
{
if(i+<s1)
return false;
}
}
return true;
}

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

3、另一种方法

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

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

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

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

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

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

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

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

leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)的更多相关文章

  1. LeetCode 551. Student Attendance Record I (C++)

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

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

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

  3. 551. Student Attendance Record I【easy】

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

  4. 【leetcode_easy】551. Student Attendance Record I

    problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...

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

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

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

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

  7. 【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则表达式 统计 日期 题目地址:https://l ...

  8. [LeetCode&Python] Problem 551. Student Attendance Record I

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

  9. 551. Student Attendance Record I + Student Attendance Record II

    ▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...

随机推荐

  1. 8-机器分配(hud4045-组合+第二类斯特林数)

    http://acm.hdu.edu.cn/showproblem.php?pid=4045 Machine schedulingTime Limit: 5000/2000 MS (Java/Othe ...

  2. sqlserver 时间函数用法

    1.DATEADD(datepart,number,date) 现在,我们希望向 "OrderDate" 添加 2 天,这样就可以找到付款日期,我们使用如下 SELECT 语句: ...

  3. Openssl CA.pl命令

    一.简介 CA.pl是证书操作的友好接口,简化了一些相似的证书创建或管理操作 二.语法 CA.pl [-?] [-h] [-help] [-newcert] [-newreq][-newreq-nod ...

  4. nltk 之 snowball 提取词干-乾颐堂

    机器学习中很重要的应用场景就是机器自动分类,而分类的关键是词干提取.所以我们要用到snowball.下面说一下snowball 提取词干的两种方法. 两种方法: 方法一: >>> f ...

  5. Python设计模式之"外观模式"实例讲解

    Python中设计模式之外观模式主张以分多模块进行代码管理而减少耦合,下面用实例来进行说明. 应用特性: 在很多复杂而小功能需要调用需求时,而且这些调用往往还有一定相关性,即一调用就是一系列的. 结构 ...

  6. Storm的StreamID使用样例(版本1.0.2)

    随手尝试了一下StreamID的的用法.留个笔记. ==数据样例== { "Address": "小桥镇小桥中学对面", "CityCode" ...

  7. Part2_lesson1---arm家族大检阅

    芯片(比如2440.6410.210等等)包含ARM核. 指令结构和ARM核有关系: ARM9对应指令架构版本ARMV4 ARM11对应指令架构版本ARMV6 cortex A8对应指令架构版本ARM ...

  8. Perl 学习笔记-正则表达式基础篇

    1.Perl中的正则表达式 在Perl中叫做模式, 是一个匹配(或不匹配)某字符串的模板, 是一种小程序, 对于一个字符串, 要么匹配, 要么不匹配. 使用简易模式: 将模式写在一对正斜线(/)中即可 ...

  9. Spring框架总结(八)

    二.Cglib代理名词理解: Cglib代理,也叫做子类代理.利用继承关系实现被代理类的功能扩展.缺点:      (1)JDK的动态代理有一个限制,就是使用动态代理的对象必须实现一个或多个接口.  ...

  10. C#和C++语言使用方面的区别

    本人觉得C#是世界上最优美的语言,也可以说是一门傻瓜语言,入门成本低,上手快得到许多人的青睐,但是C#并没有在行业内得到大家的首肯,反倒是C/C++人才比较紧俏:本人在学习过程中将C#和C++语言使用 ...