leetcode-551-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
要完成的函数:
bool checkRecord(string s)
说明:
1、这道题给定一个字符串,其中只有三种字符P/L/A,分别代表在场/迟到/缺席。如果一个学生出现了一次以上的缺席,或者连续两次以上的迟到,那么他就不能被奖励。要求判断是否某个学生能被奖励。
2、关于A的,很容易,遍历一遍字符串统计A出现次数,当次数大于1时,返回false,结束遍历。
关于L的,也不难,遍历一遍字符串,当碰到L时,判断下一个字符和再下一个字符是否均为L,如果满足,返回false,结束遍历(这里要注意边界条件,即下一个字符是否在字符串以内);如果不满足,那么继续处理下一个字符。
代码如下:
bool checkRecord(string s)
{
int counta=,countl=;
int s1=s.size();
for(int i=;i<s1;i++)
{
if(s[i]=='A')
{
counta++;
if(counta>)
return false;
}
else if(s[i+]=='L'&&s[i+]=='L'&&s[i]=='L')
{
if(i+<s1)
return false;
}
}
return true;
}
上述代码实测6ms,beats 70.11% of cpp submissions。
3、另一种方法
参考了讨论区的代码实现,发现了另一种实际花费时间更少的方法。
代码同样分享给大家,如下:
bool checkRecord(string s)
{
int counta=,countl=;
for(int i = ;i < s.size();i++)
{
if(s[i]=='A')
{
counta++;
countl=;//清空countl,重新开始
if(counta>)
return false;
}
else if(s[i]=='L')
{
countl++;
if(countl>)
return false;
}
else
countl=;
}
return true;
}
上述代码实测4ms,beats 100% of cpp submissions。
这样写代码看起来更加“清爽”,判断是否出现了连续的几个相同字符,采用的是碰到其他字符就“清空”的方法。
而2中的方法,是碰到‘L’时继续判断下一个以及再下一个字符是否仍是'L'的方式,这种方法不需要引进countl的频繁计算。
笔者还是更加喜欢“清爽”的代码,当L出现几百次才要return false的时候,明显清爽代码更省时间。
这道题目给予的启示是:当要判断字符是否连续出现时,可以采用“清空”的方法来做。
leetcode-551-Student Attendance Record I(判断是否出现连续几个相同字符)的更多相关文章
- LeetCode 551. Student Attendance Record I (C++)
题目: You are given a string representing an attendance record for a student. The record only contains ...
- LeetCode 551. Student Attendance Record I (学生出勤纪录 I)
You are given a string representing an attendance record for a student. The record only contains the ...
- 551. Student Attendance Record I【easy】
551. Student Attendance Record I[easy] You are given a string representing an attendance record for ...
- 【leetcode_easy】551. Student Attendance Record I
problem 551. Student Attendance Record I 题意: solution: class Solution { public: bool checkRecord(str ...
- 551. Student Attendance Record I 从字符串判断学生考勤
[抄题]: You are given a string representing an attendance record for a student. The record only contai ...
- [LeetCode] 552. Student Attendance Record II 学生出勤记录之二
Given a positive integer n, return the number of all possible attendance records with length n, whic ...
- 【LeetCode】551. Student Attendance Record I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则表达式 统计 日期 题目地址:https://l ...
- [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 ...
- 551. Student Attendance Record I + Student Attendance Record II
▶ 一个学生的考勤状况是一个字符串,其中各字符的含义是:A 缺勤,L 迟到,P 正常.如果一个学生考勤状况中 A 不超过一个,且没有连续两个 L(L 可以有多个,但是不能连续),则称该学生达标(原文表 ...
随机推荐
- winfrom保存图片,将文件夹中图片放入listview,与撤回操作
之前那些操作完成对图片的修改之后,就是要保存图片了. 这里保存用到一个SaveFileDialog控件,可以获取用户选择的保存文件的路径. ) { SaveFileDialog saveImageDi ...
- 图解Git
转载自http://marklodato.github.io/visual-git-guide/index-zh-cn.html#diff 图解Git 其他语言: Deutsch English Es ...
- EXTI—外部中断事件控制器
外部中断概述 STM32F4的每个IO都可以作为外部中断输入. STM32F4的中断控制器支持22个外部中断/事件请求: 从上面可以看出,STM32F4供IO使用的中断线只有16个,但是STM32F4 ...
- charles4.2下载与破解方法以及配置https.RP
Charles下载地址 地址:https://www.charlesproxy.com/latest-release/download.do 2. Charles破解 破解地址:https://www ...
- xml与java代码相互装换的工具类
这是一个java操作xml文件的工具类,最大的亮点在于能够通过工具类直接生成xml同样层次结构的java代码,也就是说,只要你定义好了xml的模板,就能一键生成java代码.省下了自己再使用工具类写代 ...
- 视觉SLAM实战(一):RGB-D SLAM V2
写在前面 首先打个广告.SLAM研究者交流QQ群:254787961.欢迎各路大神和小白前来交流. 看了前面三篇博文之后,是不是有同学要问:博主你扯了那么多有用没用的东西,能不能再给力一点,拿出一个我 ...
- sublime 3插件安装记录
安装sublime 3的package control管理器: 从菜单 View - Show Console 或者 ctrl + ~ 快捷键,调出 console.将以下 Python 代码粘贴进去 ...
- javascrip总结43:标签上自定义属性的操作
1 获取标签属性 语法: element.getAttribute('属性名') 返回对应属性的值 ,如果没有返回null. //html <div id="box" ind ...
- 搜狐 WEB 标准-前端技术应用规范
- 开源SLAM
GitHub 上优秀的开源SLAM repo (更新中):https://www.jianshu.com/p/464ca0d0c254 当前的开源SLAM方案:https://www.cnblogs. ...