描述

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

'A' : Absent.

'L' : Late.

'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

分析

P不用管,A和L分别用两个变量aCountlCount来记录。对于‘A’,每遇到一个就让aCount加一,大于等于2时直接退出主循环;对于‘L’,一旦遇到就进入一个循环,若下一个元素也为‘L’,则将lCount加1,直到不是‘L’或到了string的末尾为止。由于循环中多加了一次i,因此退出循环后要减一次i。如果此时lCount小于等于2,就置为0,否则退出主循环。

退出主循环后,判断lCount和aCount的个数,若符合条件则返回false,否则返回true。

代码如下:

class Solution {
public:
bool checkRecord(string s) {
int aCount = 0;
int lCount = 0;
for(int i = 0; i != s.size(); ++i){
if(aCount > 1)break;
if(s[i] == 'A')++aCount;
if(s[i] == 'L'){
while(s[i] == 'L' && i != s.size()){
++lCount;
++i;
}
--i;
if(lCount <= 2)lCount = 0;
else break;
}
}
if(aCount > 1 || lCount > 2)return false;
return true;
}
};

leetcode解题报告(29):Student Attendance Record I的更多相关文章

  1. [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 ...

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

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

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

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

  4. 【leetcode】552. Student Attendance Record II

    题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...

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

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

  6. 552. Student Attendance Record II

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

  7. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

  8. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

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

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

  10. 551. Student Attendance Record I【easy】

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

随机推荐

  1. 判断密码是否可见/判断登录的状态/判断在form表单中 定义rules规则验证(iview)

    一: 判断密码是否可见判断:type="visiblePassword ? 'text' : 'password'" 是否为false 或者 true 密码为输入框或者文本框点击眼 ...

  2. WPF 不要给 Window 类设置变换矩阵(分析篇):System.InvalidOperationException: 转换不可逆。

    原文:WPF 不要给 Window 类设置变换矩阵(分析篇):System.InvalidOperationException: 转换不可逆. 最近总是收到一个异常 "System.Inva ...

  3. QSDK与OPENWRT区别

    QSDK与OPENWRT区别 来源 https://www.jianshu.com/p/178ae18b2570 QSDK是一种在openwrt的基础上,加入了高通atheros芯片相关资料的一种环境 ...

  4. Java调用WebService方法总结(2)--JAX-WS调用WebService

    用JAX-WS(Java API for XML Web Services)调用WebService不需要引入其他框架,都是JDK自带的:文中所使用到的软件版本:Java 1.8.0_191.Dom4 ...

  5. rabbitmq使用教程

    检查RabbitMQ运行状态1)打开命令行命令行,进入RabbitMQ的安装目录: cd D:\installs\rabbitmq\rabbitmq_server-3.7.15\sbin2)输入 ra ...

  6. flex 布局学习

    flex 布局学习 寻根溯源话布局 一切都始于这样一个问题:怎样通过 CSS 简单而优雅的实现水平.垂直同时居中.记得刚开始学习 CSS 的时候,看到 float 属性不由得感觉眼前一亮,顺理成章的联 ...

  7. JavaScript之条件语句

    (1)if条件语句 // if(条件){当条件为真,存在即为真.当条件为false null 0 undefined中任意一种时,则表示不存在,不存在即为假} if(条件){ 条件为真时执行的代码 } ...

  8. Vivado中备份设计好的block design

    参考链接 https://blog.csdn.net/dimples_song/article/details/81391615 前言 为了不每次都重新生成block design,避免重复劳动. 可 ...

  9. 信号的有效值(RMS)估计

    % Root Mean Square Value function [retval] = rms1(sig) N = 20; for k = 1 : length(sig)/N - 1 sig_sum ...

  10. iOS加解密最重要的干货:CCCrypt

    需要引入框架#import <CommonCrypto/CommonCryptor.h> 函数定义: CCCryptorStatus CCCrypt( CCOperation op, /* ...