leetcode解题报告(29):Student Attendance Record I
描述
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分别用两个变量aCount和lCount来记录。对于‘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的更多相关文章
- [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 ...
- [LeetCode] Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- [LeetCode] 552. Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 【leetcode】552. Student Attendance Record II
题目如下: Given a positive integer n, return the number of all possible attendance records with length n ...
- [Swift]LeetCode552. 学生出勤记录 II | Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 552. Student Attendance Record II
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 551. Student Attendance Record I 从字符串判断学生考勤
[抄题]: You are given a string representing an attendance record for a student. The record only contai ...
- 551. Student Attendance Record I【easy】
551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...
随机推荐
- C# ——Parallel类
一.Parallel类 Parallel类提供了数据和任务的并行性: 二.Paraller.For() Paraller.For()方法类似于C#的for循环语句,也是多次执行一个任务.使用Paral ...
- [winfrom]C#中使用SendMessage
在C#中,程序采用了的驱动采用了事件驱动而不是原来的消息驱动,虽然.net框架提供的事件已经十分丰富,但是在以前的系统中定义了丰富的消息对系统的编程提供了方便的实现方法,因此在C#中使用消息有时候还是 ...
- 逆波兰表达式求值 java实现代码
根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总是有效的.换句话说 ...
- 使用MySQL命令行备份和恢复数据库
导出数据库: 1.windows下cmd打开命令行 2.cd 到MySQL的安装目录的bin 目录,如果bin目录添加到环境变量中不需要切换到bin目录 3.导出数据库:mysqldump -u 用户 ...
- Java调用WebService方法总结(7)--CXF调用WebService
CXF = Celtix + XFire,继承了Celtix和XFire两大开源项目的精华,是一个开源的,全功能的,容易使用的WebService框架.文中所使用到的软件版本:Java 1.8.0_1 ...
- FreeRTOS任务状态信息查询
uxTaskPriorityGet():查询某个任务的优先级 vTaskPrioritySet():改变某个任务的任务优先级 uxTaskGetSystemState():获取系统中任务状态 vTas ...
- iOS ANE植入流程
来源:http://www.adsmogo.com/help/iosANE 一.iOS ANE植入流程 Step 1:创建Flex工程 1.1 启动Flash Builder 4.6.0, 选择“Fi ...
- AOP初识
AOP是Aspect Oriented Programing的简称,面向切面编程.AOP适合于那些具有横切逻辑的应用:如性能监测,访问控制,事务管理.缓存.对象池管理以及日志记录.AOP将这些分散在各 ...
- HTTP协议复习三--TCP/IP的网络分层模型和OSI 网络分层模型
TCP/IP网络分层模型 第一层叫“链接层”(link layer),负责在以太网.WiFi这样的底层网络上发送原始数据包,工 作在网卡这个层次,使用MAC地址来标记网络上的设备,所以有时候也叫MAC ...
- Virtualization and Performance: Understanding VM Exits
翻译自:Virtualization and Performance: Understanding VM Exits 出于对特定指令 或 特定事件(eg. page fault)的响应而导致的VM ...