当前位置:网站首页>Leetcode- student attendance record i- simple

Leetcode- student attendance record i- simple

2022-06-13 05:50:00 AnWenRen

title :551 Student attendance records I- Simple

subject

Give you a string s Indicates a student's attendance record , Each character is used to mark the attendance of the day ( Absence from duty 、 late 、 Be present ). The record contains only the following three characters :

‘A’:Absent, Absence from duty
‘L’:Late, late
‘P’:Present, Be present
If students can meanwhile The following two conditions are satisfied , You can get attendance reward :

Press Total attendance meter , Student absenteeism (‘A’) Strictly Less than two days .
Student Can't There is continuity 3 Heaven or continuity 3 More than days late (‘L’) Record .
If students can get attendance rewards , return true ; otherwise , return false .

Example 1

 Input :s = "PPALLP"
 Output :true
 explain : Students are absent less than  2  Time , And it does not exist.  3  Continuous lateness record of days or more .

Example 2

 Input :s = "PPALLL"
 Output :false
 explain : The students were late for the last three days in a row , Therefore, the conditions for attendance reward are not met .

Tips

  • 1 <= s.length <= 1000
  • s[i] by 'A''L' or 'P'

Code Java

		public boolean checkRecord(String s) {
    
        StringBuilder sb = new StringBuilder(s);
        int countA = 0; //  Record absence days , if countA >= 2  return false
        int countL = 0; //  Record late days , if countL == 3  return false
        for (int i = 0; i < sb.length(); i++) {
    
            if (sb.charAt(i) == 'A') {
    
                countA ++;
                countL = 0;
                if (countA >= 2) return false;
            } else if (sb.charAt(i) == 'L') {
    
                countL ++;
                if (countL == 3) return false;
            } else countL = 0;
        }
        return true;
    }
原网站

版权声明
本文为[AnWenRen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280507174977.html