Leetcode 551 学生出勤记录1️⃣

给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:

  1. 'A' : Absent,缺勤
  2. 'L' : Late,迟到
  3. 'P' : Present,到场

如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。

你需要根据这个学生的出勤纪录判断他是否会被奖赏。

示例 1:

输入: "PPALLP"
输出: True

示例 2:

输入: "PPALLL"
输出: False

 【方法一】

class Solution {
public:
    bool checkRecord(string s) {
        return checkAbsent(s)&&checkLate(s);
    }
    bool checkAbsent(string s){
        int sum=0;
        for(auto c : s){
            if(c=='A')
                ++sum;
        }
        return sum>1?false:true;
    }
    bool checkLate(string s){
        for(int i=1;i<s.size()-1;++i){
            if(s[i]=='L'&&s[i-1]=='L'&&s[i+1]=='L')
                return false;
        }
        return true;
    }
};

【方法二】

class Solution {
public:
    bool checkRecord(string s) {
        int sumAbsent=0;
        int NumLate=0;
        for(auto c : s){
            if(c=='A'){
                if(++sumAbsent>1) return false;
                NumLate=0;
            }
            else if(c=='L'){
                if(++NumLate>2) return false;
            }
            else
                NumLate=0;
        }
        return true;
    }
};

【方法三:正则表达式匹配】

在字符串中查找不合法的情况:1.出现两个A    2.出现三个连续L 

class Solution {
public:
    bool checkRecord(string s) {
        return !regex_search(s,regex("A.*A|LLL"));
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值