leetcode (Student Attendance Record I)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85091858

Title:Student Attendance Record I    551

Difficulty:Easy

原题leetcode地址:https://leetcode.com/problems/student-attendance-record-i/

1.    注解见代码注释

时间复杂度:O(n),两次一层for循环。

空间复杂度:O(1),没有申请额外空间。

    /**
     * 先判断A,再判断L
     * @param s
     * @return
     */
    public static boolean checkRecord(String s) {

        int aCount = 0;

        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == 'A') {
                aCount++;
            }
            if (aCount >= 2) {
                return false;
            }
        }
        

        for (int i = 0; i < s.length() - 2; i++) {
            if (s.charAt(i) == 'L' && s.charAt(i + 1) == 'L' && s.charAt(i + 2) == 'L') {
                return false;
            }
        }

        return true;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85091858