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 ...
随机推荐
- Spring MVC拦截器完整代码示例
拦截器的作用: 编写一个自定义的类,实现相关拦截器接口: preHandler不放行,直接return false:直接跳转到错误页面error.jsp postHandler后置处理器,也就是C ...
- C# vb .net实现灰度化特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的灰度化呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步: ...
- font-size 你所不知道的值
说起 font-size ,大家应该都知道是做什么的: CSS 属性指定字体的大小.因为该属性的值会被用于计算em和ex长度单位,定义该值可能改变其他元素的大小. 那么font-size 的值也是多 ...
- Xen虚拟化技术详解---第四章----申请超级调用
内核驱动程序privcmd负责将位于GuestOS用户空间的超级调用请求传递到GuestOS内核中,与Linux系统的内核驱动程序相同,该操作要在系统调用ioctl()的帮助下完成. 1.关于ioct ...
- Java8新特性概览
Java8新特性简介 a)速度更快 1.对于JVM内存模型的新定义,将永久代从堆内存中移除,以前HotSpot JVM堆内存分为三块:1.年轻代 2.年老代 3.持久代 点击回顾 取而代之的是 ...
- MySQL连接查询流程源码
http://blog.itpub.net/29510932/viewspace-2129300/ 初始化: 点击(此处)折叠或打开 main |-mysqld |-my_init // 初始话线程变 ...
- 用python实现数据库查询数据方法
哈喽,好久没来了,最近搞自动化发现了很多代码弯路,特别分享出来给能用到的朋友 因为公司业务的关系,每做一笔功能冒烟测试,我们就要对很多的数据库表中的字段进行校验,当时我就想反正总是要重复的运行这些SQ ...
- mac安装openjdk8-maven-mysql-git-docker
1. openjdk8安装命令查看地址:https://github.com/AdoptOpenJDK/homebrew-openjdk#other-versions 感觉上面命令地址不靠谱,还是 ...
- http通信示例Httpclient和HttpServer
本示例源于为朋友解决一个小问题,数据库到服务器的数据传输,由于本人能力有限,暂时将它理解为从数据库中获取数取表数据,实际上有可能是文件或者其他形式的数据,不过原理都得用流传输, 首先httpclien ...
- 探索JVM底层奥秘ClassLoader源码分析
1.JVM基本结构: *.java--------javac编译------>*.class-----ClassLoad加载---->运行时数据区------->执行引擎,接口库-- ...