Java 力扣刷题(1)

        LeetcodeExercise1

package LeetcodeExercise120240818;

public class LeetcodeExercise {
    public static void main(String[] args) {
        // 需求
        //给你一个字符串 s 表示一个学生的出勤记录,其中的每个字符用来标记当天的出勤情况(缺勤、迟到、到场)。记录中只含下面三种字符
        //'A':Absent,缺勤
        //'L':Late,迟到
        //'P':Present,到场
        //如果学生能够 同时 满足下面两个条件,则可以获得出勤奖励:
        //按 总出勤 计,学生缺勤('A')严格 少于两天
        //学生 不会 存在 连续 3 天或 连续 3 天以上的迟到('L')记录
        //如果学生可以获得出勤奖励,返回 true ;否则,返回 false
        String work = "PPPPALLP";
        boolean flag = checkRecord(work);
        if (flag) {
            System.out.println("可以得到出勤奖励");
        } else {
            System.out.println("不能得到出勤奖励");
        }
    }

    // 纯暴力解法
    static public boolean checkRecord(String s) {
        int count = 0;
        int late = 0;
        char[] array = s.toCharArray();
        for (int i = 0; i < array.length; i++) {
            if (array[i] == 'A') {
                count++;
            } else if (array[i] == 'L'){
                late++;
            }
            if (count == 2) {
                return false;
            }
            if (late == 3) {
                return false;
            }
            if (i != array.length - 1) {
                if (array[i] == 'L' && array[i + 1] != 'L') {
                    late = 0;
                }
            }
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值