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;
    }
}复制代码

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

查看所有标签

猜你喜欢:

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

Game Engine Architecture, Second Edition

Game Engine Architecture, Second Edition

Jason Gregory / A K Peters/CRC Press / 2014-8-15 / USD 69.95

A 2010 CHOICE outstanding academic title, this updated book covers the theory and practice of game engine software development. It explains practical concepts and techniques used by real game studios,......一起来看看 《Game Engine Architecture, Second Edition》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具