描述

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;
}
};

最新文章

  1. OpenSUSE下编译安装OpenFoam
  2. Linux 平台静默安装 Oracle客户端
  3. django 同步数据库
  4. QM UML状态机建模实例之移植 cortex-m0
  5. Telnet
  6. SQL语言
  7. bzoj1030 [JSOI2007]文本生成器
  8. 学习MVC框架的步骤
  9. 正确导入svn拉取的工程
  10. 解决IE6不支持position:fixed;的问题
  11. 关于JS中查看当前节点的兄弟节点的使用
  12. AllocConsole
  13. Android SQLite之乐学成语项目数据库存储
  14. ArcEngine下纵断面图的绘制
  15. Linux 下搭建jsp服务器(配置jsp开发环境)
  16. margin三个值
  17. CSS3对于盒中容纳不下的内容的显示——overflow属性
  18. 简洁架构的思想,基于go实现
  19. Java 关于密码处理的工具类[MD5编码][AES加密/解密]
  20. 20175227张雪莹 2018-2019-2 《Java程序设计》第三周学习总结

热门文章

  1. js node md5模块使用问题
  2. wait(),notify(),notifyAll()必须加锁的原因
  3. ASP.NET WebAPI 连接数据库
  4. JavaJDK多任务执行框架(六)
  5. ping pathping tcping psping tracert
  6. OpenJML入门
  7. 聊聊 ES6 中的箭头函数
  8. 如何在SAP Cloud Platform ABAP编程环境里创建一个employee
  9. 部署---Apache服务器安装SSL证书
  10. vue环境搭建及创建项目