本文主要是介绍551. Student Attendance Record I[Easy](Leetcode每日一题-2021.08.17),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Problem
You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:
- ‘A’: Absent.
- ‘L’: Late.
- ‘P’: Present.
The student is eligible for an attendance award if they meet both of the following criteria:
- The student was absent (‘A’) for strictly fewer than 2 days total.
- The student was never late (‘L’) for 3 or more consecutive days.
Return true if the student is eligible for an attendance award, or false otherwise.
Constraints:
- 1 <= s.length <= 1000
- s[i] is either ‘A’, ‘L’, or ‘P’.
Example1
Input: s = “PPALLP”
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
Example2
Input: s = “PPALLL”
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
Solution
class Solution {
public:bool checkRecord(string s) {for (int i = 0, a = 0, l = 0; i < s.size(); i ++ ) {if (s[i] == 'A') a ++ , l = 0;else if (s[i] == 'L') l ++ ;else l = 0;if (a > 1 || l > 2) return false;}return true;}
};
这篇关于551. Student Attendance Record I[Easy](Leetcode每日一题-2021.08.17)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!