LeetCode每日一题: 学生出勤记录 I(No.551)

栏目: 编程工具 · 发布时间: 7年前

给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符:
'A' : Absent,缺勤
'L' : Late,迟到
'P' : Present,到场
如果一个学生的出勤记录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。
你需要根据这个学生的出勤记录判断他是否会被奖赏。
复制代码

示例:

输入: "PPALLP"
输出: True

输入: "PPALLL"
输出: False
复制代码

思考:

循环遍历字符串s,用一个aCount记录A出现次数大于1次返回false,循环判断是否又连续三个L出现,有返回false。
复制代码

实现:

class Solution {
    public boolean checkRecord(String s) {
        int aCount = 0;
        for (int count = 0; count < s.length(); count++) {
            if (s.charAt(count) == 'A' && ++aCount > 1) {
                return false;
            }
            if (count < s.length() - 2 && s.charAt(count) == 'L' && s.charAt(count + 1) == 'L'&& s.charAt(count + 2) == 'L') {
                return false;
            }
        }
        return true;
    }
}复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Handbook of Data Structures and Applications

Handbook of Data Structures and Applications

Dinesh P. Mehta / Chapman and Hall/CRC / 2004-10-28 / USD 135.95

In the late sixties, Donald Knuth, winner of the 1974Turing Award, published his landmark book The Art of Computer Programming: Fundamental Algorithms. This book brought to- gether a body of kno......一起来看看 《Handbook of Data Structures and Applications》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具