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 ...
随机推荐
- Matlab图形绘制
图形绘制 离散函数图形: 例:离散数据和离散函数可视化(离散数据作图方式) X1=[,,,,,,,,,,,,,,,,,,,]; Y1=[,,,,,,,,,,,,,,,,,,,]; figure() p ...
- 前端开发 vue,angular,react框架对比1
转载自:https://www.cnblogs.com/hubgit/p/6633214.html 首先,我们先了解什么是MVX框架模式? MVX框架模式:MVC+MVP+MVVM 1.MVC:Mod ...
- 【阿里云开发】- 安装tomcat
Tomcat配置过程 1.下载Tomcat 官网地址:http://tomcat.apache.org/ 这里我用的是apache-tomcat-8.5.38.tar.gz 2.通过ftp工具将下载好 ...
- android 给ImageView设置路径
ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上. 在UI xml定义一个ImageView如下: public void onCreate(Bundle savedI ...
- iOS 简化冗余代码
正在给深圳某家智能家居开发iPad版本,在已经存在的iPhone版上修改,该app的界面采用的是xib.xib相比代码来写界面,快速高效,但是可维护性和可读性太差.言归正传,看到这些代码后,我的心情很 ...
- live555的使用(转载)
Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等的支持.Live555实现 了对多种音视频编码格式的音视频数据的流 ...
- 批量改主机名报错:Address 192.168.43.117 maps to bogon, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
ssh连接批量修改主机名报出以下提示: [root@bqh-nfs- ~]# vim modfilyhostname.sh [root@bqh-nfs- ~]# sh modfilyhostname. ...
- mysql基础指令知识
桌面指令(cmd)进入mysql客户端 第一步:安装mysql,配置环境变量 第二步:手动开启服务 第三步:输入如下指令: mysql [-h localhost -P 3306] -u 用户名 - ...
- vmware联网解决方案:host-only共享上网
一.需求说明 vmware通过桥接的方式可以上外网,但是虚拟机的IP地址必须和物理网卡在同一网段,上网环境不同虚拟机必须跟着换ip地址很麻烦,所以最好是采用host-only方式上网. 特别是做架构实 ...
- Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤
一.AOP [知识点详解] AOP:中文名称面向切面编程 英文名称:(Aspect Oriented Programming) 正常程序执行流程都是纵向执行流程 3.1 又叫面向切面编程,在原有纵向执 ...