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

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

查看所有标签

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

Approximation Algorithms

Approximation Algorithms

Vijay V. Vazirani / Springer / 2001-07-02 / USD 54.95

'This book covers the dominant theoretical approaches to the approximate solution of hard combinatorial optimization and enumeration problems. It contains elegant combinatorial theory, useful and inte......一起来看看 《Approximation Algorithms》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试